]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/unistr_props.cpp
ICU-62107.0.1.tar.gz
[apple/icu.git] / icuSources / common / unistr_props.cpp
CommitLineData
f3c0d7a5
A
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
374ca955
A
3/*
4*******************************************************************************
5*
4388f060 6* Copyright (C) 1999-2011, International Business Machines
374ca955
A
7* Corporation and others. All Rights Reserved.
8*
9*******************************************************************************
10* file name: unistr_props.cpp
f3c0d7a5 11* encoding: UTF-8
374ca955
A
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"
4388f060 24#include "unicode/utf16.h"
374ca955 25
46f4442e
A
26U_NAMESPACE_BEGIN
27
374ca955
A
28UnicodeString&
29UnicodeString::trim()
30{
31 if(isBogus()) {
32 return *this;
33 }
34
46f4442e 35 UChar *array = getArrayStart();
374ca955 36 UChar32 c;
46f4442e
A
37 int32_t oldLength = this->length();
38 int32_t i = oldLength, length;
374ca955
A
39
40 // first cut off trailing white space
41 for(;;) {
42 length = i;
43 if(i <= 0) {
44 break;
45 }
46f4442e 46 U16_PREV(array, 0, i, c);
374ca955
A
47 if(!(c == 0x20 || u_isWhitespace(c))) {
48 break;
49 }
50 }
46f4442e
A
51 if(length < oldLength) {
52 setLength(length);
374ca955
A
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 }
46f4442e 63 U16_NEXT(array, i, length, c);
374ca955
A
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}
46f4442e
A
76
77U_NAMESPACE_END