2 *******************************************************************************
3 * Copyright (C) 2010-2011, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *******************************************************************************
6 * file name: charstr.cpp
8 * tab size: 8 (not used)
11 * created on: 2010may19
12 * created by: Markus W. Scherer
15 #include "unicode/utypes.h"
22 CharString
&CharString::copyFrom(const CharString
&s
, UErrorCode
&errorCode
) {
23 if(U_SUCCESS(errorCode
) && this!=&s
&& ensureCapacity(s
.len
+1, 0, errorCode
)) {
25 uprv_memcpy(buffer
.getAlias(), s
.buffer
.getAlias(), len
+1);
30 CharString
&CharString::truncate(int32_t newLength
) {
35 buffer
[len
=newLength
]=0;
40 CharString
&CharString::append(char c
, UErrorCode
&errorCode
) {
41 if(ensureCapacity(len
+2, 0, errorCode
)) {
48 CharString
&CharString::append(const char *s
, int32_t sLength
, UErrorCode
&errorCode
) {
49 if(U_FAILURE(errorCode
)) {
52 if(sLength
<-1 || (s
==NULL
&& sLength
!=0)) {
53 errorCode
=U_ILLEGAL_ARGUMENT_ERROR
;
57 sLength
=uprv_strlen(s
);
60 if(s
==(buffer
.getAlias()+len
)) {
61 // The caller wrote into the getAppendBuffer().
62 if(sLength
>=(buffer
.getCapacity()-len
)) {
63 // The caller wrote too much.
64 errorCode
=U_INTERNAL_PROGRAM_ERROR
;
66 buffer
[len
+=sLength
]=0;
68 } else if(buffer
.getAlias()<=s
&& s
<(buffer
.getAlias()+len
) &&
69 sLength
>=(buffer
.getCapacity()-len
)
71 // (Part of) this string is appended to itself which requires reallocation,
72 // so we have to make a copy of the substring and append that.
73 return append(CharString(s
, sLength
, errorCode
), errorCode
);
74 } else if(ensureCapacity(len
+sLength
+1, 0, errorCode
)) {
75 uprv_memcpy(buffer
.getAlias()+len
, s
, sLength
);
76 buffer
[len
+=sLength
]=0;
82 char *CharString::getAppendBuffer(int32_t minCapacity
,
83 int32_t desiredCapacityHint
,
84 int32_t &resultCapacity
,
85 UErrorCode
&errorCode
) {
86 if(U_FAILURE(errorCode
)) {
90 int32_t appendCapacity
=buffer
.getCapacity()-len
-1; // -1 for NUL
91 if(appendCapacity
>=minCapacity
) {
92 resultCapacity
=appendCapacity
;
93 return buffer
.getAlias()+len
;
95 if(ensureCapacity(len
+minCapacity
+1, len
+desiredCapacityHint
+1, errorCode
)) {
96 resultCapacity
=buffer
.getCapacity()-len
-1;
97 return buffer
.getAlias()+len
;
103 CharString
&CharString::appendInvariantChars(const UnicodeString
&s
, UErrorCode
&errorCode
) {
104 if(ensureCapacity(len
+s
.length()+1, 0, errorCode
)) {
105 len
+=s
.extract(0, 0x7fffffff, buffer
.getAlias()+len
, buffer
.getCapacity()-len
, US_INV
);
110 UBool
CharString::ensureCapacity(int32_t capacity
,
111 int32_t desiredCapacityHint
,
112 UErrorCode
&errorCode
) {
113 if(U_FAILURE(errorCode
)) {
116 if(capacity
>buffer
.getCapacity()) {
117 if(desiredCapacityHint
==0) {
118 desiredCapacityHint
=capacity
+buffer
.getCapacity();
120 if( (desiredCapacityHint
<=capacity
|| buffer
.resize(desiredCapacityHint
, len
+1)==NULL
) &&
121 buffer
.resize(capacity
, len
+1)==NULL
123 errorCode
=U_MEMORY_ALLOCATION_ERROR
;
130 CharString
&CharString::appendPathPart(const StringPiece
&s
, UErrorCode
&errorCode
) {
131 if(U_FAILURE(errorCode
)) {
138 if(len
>0 && (c
=buffer
[len
-1])!=U_FILE_SEP_CHAR
&& c
!=U_FILE_ALT_SEP_CHAR
) {
139 append(U_FILE_SEP_CHAR
, errorCode
);
141 append(s
, errorCode
);