intl/icu/source/common/charstr.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/charstr.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,145 @@
     1.4 +/*
     1.5 +*******************************************************************************
     1.6 +*   Copyright (C) 2010-2011, International Business Machines
     1.7 +*   Corporation and others.  All Rights Reserved.
     1.8 +*******************************************************************************
     1.9 +*   file name:  charstr.cpp
    1.10 +*   encoding:   US-ASCII
    1.11 +*   tab size:   8 (not used)
    1.12 +*   indentation:4
    1.13 +*
    1.14 +*   created on: 2010may19
    1.15 +*   created by: Markus W. Scherer
    1.16 +*/
    1.17 +
    1.18 +#include "unicode/utypes.h"
    1.19 +#include "charstr.h"
    1.20 +#include "cmemory.h"
    1.21 +#include "cstring.h"
    1.22 +
    1.23 +U_NAMESPACE_BEGIN
    1.24 +
    1.25 +CharString &CharString::copyFrom(const CharString &s, UErrorCode &errorCode) {
    1.26 +    if(U_SUCCESS(errorCode) && this!=&s && ensureCapacity(s.len+1, 0, errorCode)) {
    1.27 +        len=s.len;
    1.28 +        uprv_memcpy(buffer.getAlias(), s.buffer.getAlias(), len+1);
    1.29 +    }
    1.30 +    return *this;
    1.31 +}
    1.32 +
    1.33 +CharString &CharString::truncate(int32_t newLength) {
    1.34 +    if(newLength<0) {
    1.35 +        newLength=0;
    1.36 +    }
    1.37 +    if(newLength<len) {
    1.38 +        buffer[len=newLength]=0;
    1.39 +    }
    1.40 +    return *this;
    1.41 +}
    1.42 +
    1.43 +CharString &CharString::append(char c, UErrorCode &errorCode) {
    1.44 +    if(ensureCapacity(len+2, 0, errorCode)) {
    1.45 +        buffer[len++]=c;
    1.46 +        buffer[len]=0;
    1.47 +    }
    1.48 +    return *this;
    1.49 +}
    1.50 +
    1.51 +CharString &CharString::append(const char *s, int32_t sLength, UErrorCode &errorCode) {
    1.52 +    if(U_FAILURE(errorCode)) {
    1.53 +        return *this;
    1.54 +    }
    1.55 +    if(sLength<-1 || (s==NULL && sLength!=0)) {
    1.56 +        errorCode=U_ILLEGAL_ARGUMENT_ERROR;
    1.57 +        return *this;
    1.58 +    }
    1.59 +    if(sLength<0) {
    1.60 +        sLength=uprv_strlen(s);
    1.61 +    }
    1.62 +    if(sLength>0) {
    1.63 +        if(s==(buffer.getAlias()+len)) {
    1.64 +            // The caller wrote into the getAppendBuffer().
    1.65 +            if(sLength>=(buffer.getCapacity()-len)) {
    1.66 +                // The caller wrote too much.
    1.67 +                errorCode=U_INTERNAL_PROGRAM_ERROR;
    1.68 +            } else {
    1.69 +                buffer[len+=sLength]=0;
    1.70 +            }
    1.71 +        } else if(buffer.getAlias()<=s && s<(buffer.getAlias()+len) &&
    1.72 +                  sLength>=(buffer.getCapacity()-len)
    1.73 +        ) {
    1.74 +            // (Part of) this string is appended to itself which requires reallocation,
    1.75 +            // so we have to make a copy of the substring and append that.
    1.76 +            return append(CharString(s, sLength, errorCode), errorCode);
    1.77 +        } else if(ensureCapacity(len+sLength+1, 0, errorCode)) {
    1.78 +            uprv_memcpy(buffer.getAlias()+len, s, sLength);
    1.79 +            buffer[len+=sLength]=0;
    1.80 +        }
    1.81 +    }
    1.82 +    return *this;
    1.83 +}
    1.84 +
    1.85 +char *CharString::getAppendBuffer(int32_t minCapacity,
    1.86 +                                  int32_t desiredCapacityHint,
    1.87 +                                  int32_t &resultCapacity,
    1.88 +                                  UErrorCode &errorCode) {
    1.89 +    if(U_FAILURE(errorCode)) {
    1.90 +        resultCapacity=0;
    1.91 +        return NULL;
    1.92 +    }
    1.93 +    int32_t appendCapacity=buffer.getCapacity()-len-1;  // -1 for NUL
    1.94 +    if(appendCapacity>=minCapacity) {
    1.95 +        resultCapacity=appendCapacity;
    1.96 +        return buffer.getAlias()+len;
    1.97 +    }
    1.98 +    if(ensureCapacity(len+minCapacity+1, len+desiredCapacityHint+1, errorCode)) {
    1.99 +        resultCapacity=buffer.getCapacity()-len-1;
   1.100 +        return buffer.getAlias()+len;
   1.101 +    }
   1.102 +    resultCapacity=0;
   1.103 +    return NULL;
   1.104 +}
   1.105 +
   1.106 +CharString &CharString::appendInvariantChars(const UnicodeString &s, UErrorCode &errorCode) {
   1.107 +    if(ensureCapacity(len+s.length()+1, 0, errorCode)) {
   1.108 +        len+=s.extract(0, 0x7fffffff, buffer.getAlias()+len, buffer.getCapacity()-len, US_INV);
   1.109 +    }
   1.110 +    return *this;
   1.111 +}
   1.112 +
   1.113 +UBool CharString::ensureCapacity(int32_t capacity,
   1.114 +                                 int32_t desiredCapacityHint,
   1.115 +                                 UErrorCode &errorCode) {
   1.116 +    if(U_FAILURE(errorCode)) {
   1.117 +        return FALSE;
   1.118 +    }
   1.119 +    if(capacity>buffer.getCapacity()) {
   1.120 +        if(desiredCapacityHint==0) {
   1.121 +            desiredCapacityHint=capacity+buffer.getCapacity();
   1.122 +        }
   1.123 +        if( (desiredCapacityHint<=capacity || buffer.resize(desiredCapacityHint, len+1)==NULL) &&
   1.124 +            buffer.resize(capacity, len+1)==NULL
   1.125 +        ) {
   1.126 +            errorCode=U_MEMORY_ALLOCATION_ERROR;
   1.127 +            return FALSE;
   1.128 +        }
   1.129 +    }
   1.130 +    return TRUE;
   1.131 +}
   1.132 +
   1.133 +CharString &CharString::appendPathPart(const StringPiece &s, UErrorCode &errorCode) {
   1.134 +    if(U_FAILURE(errorCode)) {
   1.135 +        return *this;
   1.136 +    }
   1.137 +    if(s.length()==0) {
   1.138 +        return *this;
   1.139 +    }
   1.140 +    char c;
   1.141 +    if(len>0 && (c=buffer[len-1])!=U_FILE_SEP_CHAR && c!=U_FILE_ALT_SEP_CHAR) {
   1.142 +        append(U_FILE_SEP_CHAR, errorCode);
   1.143 +    }
   1.144 +    append(s, errorCode);
   1.145 +    return *this;
   1.146 +}
   1.147 +
   1.148 +U_NAMESPACE_END

mercurial