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 10 StringUtils::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 33 StringUtils::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*/ int 43 StringUtils::NullSafeCompare(const char* s1, const char* s2) 44 { 45 if ((NULL == s1) && (NULL == s2)) 46 return 0; 47 if (NULL == s1) 48 return 1; 49 if (NULL == s2) 50 return -1; 51 return strcmp(s1, s2); 52 } 53 54 55 /*static*/ bool 56 StringUtils::_IsSpaceOrControl(char ch) 57 { 58 return ch <= 32; 59 } 60