• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 ******************************************************************************
3 *
4 *   Copyright (C) 2010, International Business Machines
5 *   Corporation and others.  All Rights Reserved.
6 *
7 ******************************************************************************
8 *
9 * File decnumstr.cpp
10 *
11 */
12 
13 #include "unicode/utypes.h"
14 #include "decnumstr.h"
15 #include "cmemory.h"
16 #include "uassert.h"
17 
18 U_NAMESPACE_BEGIN
19 
DecimalNumberString()20 DecimalNumberString::DecimalNumberString() {
21     fLength  = 0;
22     fText[0] = 0;
23 }
24 
~DecimalNumberString()25 DecimalNumberString::~DecimalNumberString() {
26 }
27 
DecimalNumberString(const StringPiece & source,UErrorCode & status)28 DecimalNumberString::DecimalNumberString(const StringPiece &source, UErrorCode &status) {
29     fLength = 0;
30     fText[0] = 0;
31     append(source, status);
32 }
33 
append(char c,UErrorCode & status)34 DecimalNumberString & DecimalNumberString::append(char c, UErrorCode &status) {
35     if (ensureCapacity(fLength + 2, status) == FALSE) {
36         return *this;
37     }
38     fText[fLength++] = c;
39     fText[fLength] = 0;
40     return *this;
41 }
42 
append(const StringPiece & str,UErrorCode & status)43 DecimalNumberString &DecimalNumberString::append(const StringPiece &str, UErrorCode &status) {
44     int32_t sLength = str.length();
45     if (ensureCapacity(fLength + sLength + 1, status) == FALSE) {
46         return *this;
47     }
48     uprv_memcpy(&fText[fLength], str.data(), sLength);
49     fLength += sLength;
50     fText[fLength] = 0;
51     return *this;
52 }
53 
operator [](int32_t index)54 char & DecimalNumberString::operator [] (int32_t index) {
55     U_ASSERT(index>=0 && index<fLength);
56     return fText[index];
57 }
58 
operator [](int32_t index) const59 const char & DecimalNumberString::operator [] (int32_t index) const {
60     U_ASSERT(index>=0 && index<fLength);
61     return fText[index];
62 }
63 
length() const64 int32_t DecimalNumberString::length() const {
65     return fLength;
66 }
67 
setLength(int32_t length,UErrorCode & status)68 void DecimalNumberString::setLength(int32_t length, UErrorCode &status) {
69     if (ensureCapacity(length+1, status) == FALSE) {
70         return;
71     }
72     if (length > fLength) {
73         uprv_memset(&fText[fLength], length - fLength, 0);
74     }
75     fLength = length;
76     fText[fLength] = 0;
77 }
78 
operator StringPiece() const79 DecimalNumberString::operator StringPiece() const {
80     return StringPiece(fText, fLength);
81 }
82 
ensureCapacity(int32_t neededSize,UErrorCode & status)83 UBool DecimalNumberString::ensureCapacity(int32_t neededSize, UErrorCode &status) {
84     if (U_FAILURE(status)) {
85         return FALSE;
86     }
87     if (fText.getCapacity() < neededSize) {
88         char *newBuf = fText.resize(neededSize, fText.getCapacity());
89         if (newBuf == NULL) {
90             status = U_MEMORY_ALLOCATION_ERROR;
91             return FALSE;
92         }
93         U_ASSERT(fText.getCapacity() >= neededSize);
94         U_ASSERT(fText.getAlias() == newBuf);
95     }
96     return TRUE;
97 }
98 
99 U_NAMESPACE_END
100 
101