intl/icu/source/common/charstr.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/charstr.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,130 @@
     1.4 +/*
     1.5 +**********************************************************************
     1.6 +*   Copyright (c) 2001-2012, International Business Machines
     1.7 +*   Corporation and others.  All Rights Reserved.
     1.8 +**********************************************************************
     1.9 +*   Date        Name        Description
    1.10 +*   11/19/2001  aliu        Creation.
    1.11 +*   05/19/2010  markus      Rewritten from scratch
    1.12 +**********************************************************************
    1.13 +*/
    1.14 +
    1.15 +#ifndef CHARSTRING_H
    1.16 +#define CHARSTRING_H
    1.17 +
    1.18 +#include "unicode/utypes.h"
    1.19 +#include "unicode/unistr.h"
    1.20 +#include "unicode/uobject.h"
    1.21 +#include "cmemory.h"
    1.22 +
    1.23 +U_NAMESPACE_BEGIN
    1.24 +
    1.25 +// Windows needs us to DLL-export the MaybeStackArray template specialization,
    1.26 +// but MacOS X cannot handle it. Same as in digitlst.h.
    1.27 +#if !U_PLATFORM_IS_DARWIN_BASED
    1.28 +template class U_COMMON_API MaybeStackArray<char, 40>;
    1.29 +#endif
    1.30 +
    1.31 +/**
    1.32 + * ICU-internal char * string class.
    1.33 + * This class does not assume or enforce any particular character encoding.
    1.34 + * Raw bytes can be stored. The string object owns its characters.
    1.35 + * A terminating NUL is stored, but the class does not prevent embedded NUL characters.
    1.36 + *
    1.37 + * This class wants to be convenient but is also deliberately minimalist.
    1.38 + * Please do not add methods if they only add minor convenience.
    1.39 + * For example:
    1.40 + *   cs.data()[5]='a';  // no need for setCharAt(5, 'a')
    1.41 + */
    1.42 +class U_COMMON_API CharString : public UMemory {
    1.43 +public:
    1.44 +    CharString() : len(0) { buffer[0]=0; }
    1.45 +    CharString(const StringPiece &s, UErrorCode &errorCode) : len(0) {
    1.46 +        buffer[0]=0;
    1.47 +        append(s, errorCode);
    1.48 +    }
    1.49 +    CharString(const CharString &s, UErrorCode &errorCode) : len(0) {
    1.50 +        buffer[0]=0;
    1.51 +        append(s, errorCode);
    1.52 +    }
    1.53 +    CharString(const char *s, int32_t sLength, UErrorCode &errorCode) : len(0) {
    1.54 +        buffer[0]=0;
    1.55 +        append(s, sLength, errorCode);
    1.56 +    }
    1.57 +    ~CharString() {}
    1.58 +
    1.59 +    /**
    1.60 +     * Replaces this string's contents with the other string's contents.
    1.61 +     * CharString does not support the standard copy constructor nor
    1.62 +     * the assignment operator, to make copies explicit and to
    1.63 +     * use a UErrorCode where memory allocations might be needed.
    1.64 +     */
    1.65 +    CharString &copyFrom(const CharString &other, UErrorCode &errorCode);
    1.66 +
    1.67 +    UBool isEmpty() const { return len==0; }
    1.68 +    int32_t length() const { return len; }
    1.69 +    char operator[](int32_t index) const { return buffer[index]; }
    1.70 +    StringPiece toStringPiece() const { return StringPiece(buffer.getAlias(), len); }
    1.71 +
    1.72 +    const char *data() const { return buffer.getAlias(); }
    1.73 +    char *data() { return buffer.getAlias(); }
    1.74 +
    1.75 +    CharString &clear() { len=0; buffer[0]=0; return *this; }
    1.76 +    CharString &truncate(int32_t newLength);
    1.77 +
    1.78 +    CharString &append(char c, UErrorCode &errorCode);
    1.79 +    CharString &append(const StringPiece &s, UErrorCode &errorCode) {
    1.80 +        return append(s.data(), s.length(), errorCode);
    1.81 +    }
    1.82 +    CharString &append(const CharString &s, UErrorCode &errorCode) {
    1.83 +        return append(s.data(), s.length(), errorCode);
    1.84 +    }
    1.85 +    CharString &append(const char *s, int32_t sLength, UErrorCode &status);
    1.86 +    /**
    1.87 +     * Returns a writable buffer for appending and writes the buffer's capacity to
    1.88 +     * resultCapacity. Guarantees resultCapacity>=minCapacity if U_SUCCESS().
    1.89 +     * There will additionally be space for a terminating NUL right at resultCapacity.
    1.90 +     * (This function is similar to ByteSink.GetAppendBuffer().)
    1.91 +     *
    1.92 +     * The returned buffer is only valid until the next write operation
    1.93 +     * on this string.
    1.94 +     *
    1.95 +     * After writing at most resultCapacity bytes, call append() with the
    1.96 +     * pointer returned from this function and the number of bytes written.
    1.97 +     *
    1.98 +     * @param minCapacity required minimum capacity of the returned buffer;
    1.99 +     *                    must be non-negative
   1.100 +     * @param desiredCapacityHint desired capacity of the returned buffer;
   1.101 +     *                            must be non-negative
   1.102 +     * @param resultCapacity will be set to the capacity of the returned buffer
   1.103 +     * @param errorCode in/out error code
   1.104 +     * @return a buffer with resultCapacity>=min_capacity
   1.105 +     */
   1.106 +    char *getAppendBuffer(int32_t minCapacity,
   1.107 +                          int32_t desiredCapacityHint,
   1.108 +                          int32_t &resultCapacity,
   1.109 +                          UErrorCode &errorCode);
   1.110 +
   1.111 +    CharString &appendInvariantChars(const UnicodeString &s, UErrorCode &errorCode);
   1.112 +
   1.113 +    /**
   1.114 +     * Appends a filename/path part, e.g., a directory name.
   1.115 +     * First appends a U_FILE_SEP_CHAR if necessary.
   1.116 +     * Does nothing if s is empty.
   1.117 +     */
   1.118 +    CharString &appendPathPart(const StringPiece &s, UErrorCode &errorCode);
   1.119 +
   1.120 +private:
   1.121 +    MaybeStackArray<char, 40> buffer;
   1.122 +    int32_t len;
   1.123 +
   1.124 +    UBool ensureCapacity(int32_t capacity, int32_t desiredCapacityHint, UErrorCode &errorCode);
   1.125 +
   1.126 +    CharString(const CharString &other); // forbid copying of this class
   1.127 +    CharString &operator=(const CharString &other); // forbid copying of this class
   1.128 +};
   1.129 +
   1.130 +U_NAMESPACE_END
   1.131 +
   1.132 +#endif
   1.133 +//eof

mercurial