1/*
2 * Copyright 2023, Andrew Lindesay <apl@lindesay.co.nz>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6#include "StringUtils.h"
7
8
9/*static*/ void
10StringUtils::InSituTrimSpaceAndControl(BString& value)
11{
12	int i = 0;
13	int len = value.Length();
14
15	while (i < len && _IsSpaceOrControl(value.ByteAt(i)))
16		i ++;
17
18	if (i != 0)
19		value.Remove(0, i);
20
21	len = value.Length();
22	i = len - 1;
23
24	while (i > 0 && _IsSpaceOrControl(value.ByteAt(i)))
25		i --;
26
27	if (i != (len - 1))
28		value.Remove(i + 1, (len - (i + 1)));
29}
30
31
32/*static*/ void
33StringUtils::InSituStripSpaceAndControl(BString& value)
34{
35	for (int i = value.Length() - 1; i >= 0; i--) {
36		if (_IsSpaceOrControl(value.ByteAt(i)))
37			value.Remove(i, 1);
38	}
39}
40
41
42/*static*/ bool
43StringUtils::_IsSpaceOrControl(char ch)
44{
45	return ch <= 32;
46}
47