1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 *   Copyright (C) 1999-2011, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 *******************************************************************************
10 *   file name:  unistr_props.cpp
11 *   encoding:   UTF-8
12 *   tab size:   8 (not used)
13 *   indentation:2
14 *
15 *   created on: 2004aug25
16 *   created by: Markus W. Scherer
17 *
18 *   Character property dependent functions moved here from unistr.cpp
19 */
20 
21 #include "unicode/utypes.h"
22 #include "unicode/uchar.h"
23 #include "unicode/unistr.h"
24 #include "unicode/utf16.h"
25 
26 U_NAMESPACE_BEGIN
27 
28 UnicodeString&
trim()29 UnicodeString::trim()
30 {
31   if(isBogus()) {
32     return *this;
33   }
34 
35   UChar *array = getArrayStart();
36   UChar32 c;
37   int32_t oldLength = this->length();
38   int32_t i = oldLength, length;
39 
40   // first cut off trailing white space
41   for(;;) {
42     length = i;
43     if(i <= 0) {
44       break;
45     }
46     U16_PREV(array, 0, i, c);
47     if(!(c == 0x20 || u_isWhitespace(c))) {
48       break;
49     }
50   }
51   if(length < oldLength) {
52     setLength(length);
53   }
54 
55   // find leading white space
56   int32_t start;
57   i = 0;
58   for(;;) {
59     start = i;
60     if(i >= length) {
61       break;
62     }
63     U16_NEXT(array, i, length, c);
64     if(!(c == 0x20 || u_isWhitespace(c))) {
65       break;
66     }
67   }
68 
69   // move string forward over leading white space
70   if(start > 0) {
71     doReplace(0, start, 0, 0, 0);
72   }
73 
74   return *this;
75 }
76 
77 U_NAMESPACE_END
78