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