Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * Copyright (C) 2010-2011, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ******************************************************************************* |
michael@0 | 6 | * file name: charstr.cpp |
michael@0 | 7 | * encoding: US-ASCII |
michael@0 | 8 | * tab size: 8 (not used) |
michael@0 | 9 | * indentation:4 |
michael@0 | 10 | * |
michael@0 | 11 | * created on: 2010may19 |
michael@0 | 12 | * created by: Markus W. Scherer |
michael@0 | 13 | */ |
michael@0 | 14 | |
michael@0 | 15 | #include "unicode/utypes.h" |
michael@0 | 16 | #include "charstr.h" |
michael@0 | 17 | #include "cmemory.h" |
michael@0 | 18 | #include "cstring.h" |
michael@0 | 19 | |
michael@0 | 20 | U_NAMESPACE_BEGIN |
michael@0 | 21 | |
michael@0 | 22 | CharString &CharString::copyFrom(const CharString &s, UErrorCode &errorCode) { |
michael@0 | 23 | if(U_SUCCESS(errorCode) && this!=&s && ensureCapacity(s.len+1, 0, errorCode)) { |
michael@0 | 24 | len=s.len; |
michael@0 | 25 | uprv_memcpy(buffer.getAlias(), s.buffer.getAlias(), len+1); |
michael@0 | 26 | } |
michael@0 | 27 | return *this; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | CharString &CharString::truncate(int32_t newLength) { |
michael@0 | 31 | if(newLength<0) { |
michael@0 | 32 | newLength=0; |
michael@0 | 33 | } |
michael@0 | 34 | if(newLength<len) { |
michael@0 | 35 | buffer[len=newLength]=0; |
michael@0 | 36 | } |
michael@0 | 37 | return *this; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | CharString &CharString::append(char c, UErrorCode &errorCode) { |
michael@0 | 41 | if(ensureCapacity(len+2, 0, errorCode)) { |
michael@0 | 42 | buffer[len++]=c; |
michael@0 | 43 | buffer[len]=0; |
michael@0 | 44 | } |
michael@0 | 45 | return *this; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | CharString &CharString::append(const char *s, int32_t sLength, UErrorCode &errorCode) { |
michael@0 | 49 | if(U_FAILURE(errorCode)) { |
michael@0 | 50 | return *this; |
michael@0 | 51 | } |
michael@0 | 52 | if(sLength<-1 || (s==NULL && sLength!=0)) { |
michael@0 | 53 | errorCode=U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 54 | return *this; |
michael@0 | 55 | } |
michael@0 | 56 | if(sLength<0) { |
michael@0 | 57 | sLength=uprv_strlen(s); |
michael@0 | 58 | } |
michael@0 | 59 | if(sLength>0) { |
michael@0 | 60 | if(s==(buffer.getAlias()+len)) { |
michael@0 | 61 | // The caller wrote into the getAppendBuffer(). |
michael@0 | 62 | if(sLength>=(buffer.getCapacity()-len)) { |
michael@0 | 63 | // The caller wrote too much. |
michael@0 | 64 | errorCode=U_INTERNAL_PROGRAM_ERROR; |
michael@0 | 65 | } else { |
michael@0 | 66 | buffer[len+=sLength]=0; |
michael@0 | 67 | } |
michael@0 | 68 | } else if(buffer.getAlias()<=s && s<(buffer.getAlias()+len) && |
michael@0 | 69 | sLength>=(buffer.getCapacity()-len) |
michael@0 | 70 | ) { |
michael@0 | 71 | // (Part of) this string is appended to itself which requires reallocation, |
michael@0 | 72 | // so we have to make a copy of the substring and append that. |
michael@0 | 73 | return append(CharString(s, sLength, errorCode), errorCode); |
michael@0 | 74 | } else if(ensureCapacity(len+sLength+1, 0, errorCode)) { |
michael@0 | 75 | uprv_memcpy(buffer.getAlias()+len, s, sLength); |
michael@0 | 76 | buffer[len+=sLength]=0; |
michael@0 | 77 | } |
michael@0 | 78 | } |
michael@0 | 79 | return *this; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | char *CharString::getAppendBuffer(int32_t minCapacity, |
michael@0 | 83 | int32_t desiredCapacityHint, |
michael@0 | 84 | int32_t &resultCapacity, |
michael@0 | 85 | UErrorCode &errorCode) { |
michael@0 | 86 | if(U_FAILURE(errorCode)) { |
michael@0 | 87 | resultCapacity=0; |
michael@0 | 88 | return NULL; |
michael@0 | 89 | } |
michael@0 | 90 | int32_t appendCapacity=buffer.getCapacity()-len-1; // -1 for NUL |
michael@0 | 91 | if(appendCapacity>=minCapacity) { |
michael@0 | 92 | resultCapacity=appendCapacity; |
michael@0 | 93 | return buffer.getAlias()+len; |
michael@0 | 94 | } |
michael@0 | 95 | if(ensureCapacity(len+minCapacity+1, len+desiredCapacityHint+1, errorCode)) { |
michael@0 | 96 | resultCapacity=buffer.getCapacity()-len-1; |
michael@0 | 97 | return buffer.getAlias()+len; |
michael@0 | 98 | } |
michael@0 | 99 | resultCapacity=0; |
michael@0 | 100 | return NULL; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | CharString &CharString::appendInvariantChars(const UnicodeString &s, UErrorCode &errorCode) { |
michael@0 | 104 | if(ensureCapacity(len+s.length()+1, 0, errorCode)) { |
michael@0 | 105 | len+=s.extract(0, 0x7fffffff, buffer.getAlias()+len, buffer.getCapacity()-len, US_INV); |
michael@0 | 106 | } |
michael@0 | 107 | return *this; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | UBool CharString::ensureCapacity(int32_t capacity, |
michael@0 | 111 | int32_t desiredCapacityHint, |
michael@0 | 112 | UErrorCode &errorCode) { |
michael@0 | 113 | if(U_FAILURE(errorCode)) { |
michael@0 | 114 | return FALSE; |
michael@0 | 115 | } |
michael@0 | 116 | if(capacity>buffer.getCapacity()) { |
michael@0 | 117 | if(desiredCapacityHint==0) { |
michael@0 | 118 | desiredCapacityHint=capacity+buffer.getCapacity(); |
michael@0 | 119 | } |
michael@0 | 120 | if( (desiredCapacityHint<=capacity || buffer.resize(desiredCapacityHint, len+1)==NULL) && |
michael@0 | 121 | buffer.resize(capacity, len+1)==NULL |
michael@0 | 122 | ) { |
michael@0 | 123 | errorCode=U_MEMORY_ALLOCATION_ERROR; |
michael@0 | 124 | return FALSE; |
michael@0 | 125 | } |
michael@0 | 126 | } |
michael@0 | 127 | return TRUE; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | CharString &CharString::appendPathPart(const StringPiece &s, UErrorCode &errorCode) { |
michael@0 | 131 | if(U_FAILURE(errorCode)) { |
michael@0 | 132 | return *this; |
michael@0 | 133 | } |
michael@0 | 134 | if(s.length()==0) { |
michael@0 | 135 | return *this; |
michael@0 | 136 | } |
michael@0 | 137 | char c; |
michael@0 | 138 | if(len>0 && (c=buffer[len-1])!=U_FILE_SEP_CHAR && c!=U_FILE_ALT_SEP_CHAR) { |
michael@0 | 139 | append(U_FILE_SEP_CHAR, errorCode); |
michael@0 | 140 | } |
michael@0 | 141 | append(s, errorCode); |
michael@0 | 142 | return *this; |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | U_NAMESPACE_END |