1/*
2*******************************************************************************
3*
4*   Copyright (C) 1999-2011, International Business Machines
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"
22#include "unicode/utf16.h"
23
24U_NAMESPACE_BEGIN
25
26UnicodeString&
27UnicodeString::trim()
28{
29  if(isBogus()) {
30    return *this;
31  }
32
33  UChar *array = getArrayStart();
34  UChar32 c;
35  int32_t oldLength = this->length();
36  int32_t i = oldLength, length;
37
38  // first cut off trailing white space
39  for(;;) {
40    length = i;
41    if(i <= 0) {
42      break;
43    }
44    U16_PREV(array, 0, i, c);
45    if(!(c == 0x20 || u_isWhitespace(c))) {
46      break;
47    }
48  }
49  if(length < oldLength) {
50    setLength(length);
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    }
61    U16_NEXT(array, i, length, c);
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}
74
75U_NAMESPACE_END
76