gfx/skia/trunk/include/core/SkString.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/include/core/SkString.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,247 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2006 The Android Open Source Project
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#ifndef SkString_DEFINED
    1.14 +#define SkString_DEFINED
    1.15 +
    1.16 +#include "SkScalar.h"
    1.17 +#include "SkTArray.h"
    1.18 +
    1.19 +#include <stdarg.h>
    1.20 +
    1.21 +/*  Some helper functions for C strings
    1.22 +*/
    1.23 +
    1.24 +static bool SkStrStartsWith(const char string[], const char prefixStr[]) {
    1.25 +    SkASSERT(string);
    1.26 +    SkASSERT(prefixStr);
    1.27 +    return !strncmp(string, prefixStr, strlen(prefixStr));
    1.28 +}
    1.29 +static bool SkStrStartsWith(const char string[], const char prefixChar) {
    1.30 +    SkASSERT(string);
    1.31 +    return (prefixChar == *string);
    1.32 +}
    1.33 +
    1.34 +bool SkStrEndsWith(const char string[], const char suffixStr[]);
    1.35 +bool SkStrEndsWith(const char string[], const char suffixChar);
    1.36 +
    1.37 +int SkStrStartsWithOneOf(const char string[], const char prefixes[]);
    1.38 +
    1.39 +static int SkStrFind(const char string[], const char substring[]) {
    1.40 +    const char *first = strstr(string, substring);
    1.41 +    if (NULL == first) return -1;
    1.42 +    return SkToS32(first - &string[0]);
    1.43 +}
    1.44 +
    1.45 +static bool SkStrContains(const char string[], const char substring[]) {
    1.46 +    SkASSERT(string);
    1.47 +    SkASSERT(substring);
    1.48 +    return (-1 != SkStrFind(string, substring));
    1.49 +}
    1.50 +static bool SkStrContains(const char string[], const char subchar) {
    1.51 +    SkASSERT(string);
    1.52 +    char tmp[2];
    1.53 +    tmp[0] = subchar;
    1.54 +    tmp[1] = '\0';
    1.55 +    return (-1 != SkStrFind(string, tmp));
    1.56 +}
    1.57 +
    1.58 +static inline char *SkStrDup(const char string[]) {
    1.59 +    char *ret = (char *) sk_malloc_throw(strlen(string)+1);
    1.60 +    memcpy(ret,string,strlen(string)+1);
    1.61 +    return ret;
    1.62 +}
    1.63 +
    1.64 +
    1.65 +
    1.66 +#define SkStrAppendU32_MaxSize  10
    1.67 +char*   SkStrAppendU32(char buffer[], uint32_t);
    1.68 +#define SkStrAppendU64_MaxSize  20
    1.69 +char*   SkStrAppendU64(char buffer[], uint64_t, int minDigits);
    1.70 +
    1.71 +#define SkStrAppendS32_MaxSize  (SkStrAppendU32_MaxSize + 1)
    1.72 +char*   SkStrAppendS32(char buffer[], int32_t);
    1.73 +#define SkStrAppendS64_MaxSize  (SkStrAppendU64_MaxSize + 1)
    1.74 +char*   SkStrAppendS64(char buffer[], int64_t, int minDigits);
    1.75 +
    1.76 +/**
    1.77 + *  Floats have at most 8 significant digits, so we limit our %g to that.
    1.78 + *  However, the total string could be 15 characters: -1.2345678e-005
    1.79 + *
    1.80 + *  In theory we should only expect up to 2 digits for the exponent, but on
    1.81 + *  some platforms we have seen 3 (as in the example above).
    1.82 + */
    1.83 +#define SkStrAppendScalar_MaxSize  15
    1.84 +
    1.85 +/**
    1.86 + *  Write the scaler in decimal format into buffer, and return a pointer to
    1.87 + *  the next char after the last one written. Note: a terminating 0 is not
    1.88 + *  written into buffer, which must be at least SkStrAppendScalar_MaxSize.
    1.89 + *  Thus if the caller wants to add a 0 at the end, buffer must be at least
    1.90 + *  SkStrAppendScalar_MaxSize + 1 bytes large.
    1.91 + */
    1.92 +#define SkStrAppendScalar SkStrAppendFloat
    1.93 +
    1.94 +char* SkStrAppendFloat(char buffer[], float);
    1.95 +char* SkStrAppendFixed(char buffer[], SkFixed);
    1.96 +
    1.97 +/** \class SkString
    1.98 +
    1.99 +    Light weight class for managing strings. Uses reference
   1.100 +    counting to make string assignments and copies very fast
   1.101 +    with no extra RAM cost. Assumes UTF8 encoding.
   1.102 +*/
   1.103 +class SK_API SkString {
   1.104 +public:
   1.105 +                SkString();
   1.106 +    explicit    SkString(size_t len);
   1.107 +    explicit    SkString(const char text[]);
   1.108 +                SkString(const char text[], size_t len);
   1.109 +                SkString(const SkString&);
   1.110 +                ~SkString();
   1.111 +
   1.112 +    bool        isEmpty() const { return 0 == fRec->fLength; }
   1.113 +    size_t      size() const { return (size_t) fRec->fLength; }
   1.114 +    const char* c_str() const { return fRec->data(); }
   1.115 +    char operator[](size_t n) const { return this->c_str()[n]; }
   1.116 +
   1.117 +    bool equals(const SkString&) const;
   1.118 +    bool equals(const char text[]) const;
   1.119 +    bool equals(const char text[], size_t len) const;
   1.120 +
   1.121 +    bool startsWith(const char prefixStr[]) const {
   1.122 +        return SkStrStartsWith(fRec->data(), prefixStr);
   1.123 +    }
   1.124 +    bool startsWith(const char prefixChar) const {
   1.125 +        return SkStrStartsWith(fRec->data(), prefixChar);
   1.126 +    }
   1.127 +    bool endsWith(const char suffixStr[]) const {
   1.128 +        return SkStrEndsWith(fRec->data(), suffixStr);
   1.129 +    }
   1.130 +    bool endsWith(const char suffixChar) const {
   1.131 +        return SkStrEndsWith(fRec->data(), suffixChar);
   1.132 +    }
   1.133 +    bool contains(const char substring[]) const {
   1.134 +        return SkStrContains(fRec->data(), substring);
   1.135 +    }
   1.136 +    bool contains(const char subchar) const {
   1.137 +        return SkStrContains(fRec->data(), subchar);
   1.138 +    }
   1.139 +    int find(const char substring[]) const {
   1.140 +        return SkStrFind(fRec->data(), substring);
   1.141 +    }
   1.142 +
   1.143 +    friend bool operator==(const SkString& a, const SkString& b) {
   1.144 +        return a.equals(b);
   1.145 +    }
   1.146 +    friend bool operator!=(const SkString& a, const SkString& b) {
   1.147 +        return !a.equals(b);
   1.148 +    }
   1.149 +
   1.150 +    // these methods edit the string
   1.151 +
   1.152 +    SkString& operator=(const SkString&);
   1.153 +    SkString& operator=(const char text[]);
   1.154 +
   1.155 +    char* writable_str();
   1.156 +    char& operator[](size_t n) { return this->writable_str()[n]; }
   1.157 +
   1.158 +    void reset();
   1.159 +    void resize(size_t len) { this->set(NULL, len); }
   1.160 +    void set(const SkString& src) { *this = src; }
   1.161 +    void set(const char text[]);
   1.162 +    void set(const char text[], size_t len);
   1.163 +    void setUTF16(const uint16_t[]);
   1.164 +    void setUTF16(const uint16_t[], size_t len);
   1.165 +
   1.166 +    void insert(size_t offset, const SkString& src) { this->insert(offset, src.c_str(), src.size()); }
   1.167 +    void insert(size_t offset, const char text[]);
   1.168 +    void insert(size_t offset, const char text[], size_t len);
   1.169 +    void insertUnichar(size_t offset, SkUnichar);
   1.170 +    void insertS32(size_t offset, int32_t value);
   1.171 +    void insertS64(size_t offset, int64_t value, int minDigits = 0);
   1.172 +    void insertU32(size_t offset, uint32_t value);
   1.173 +    void insertU64(size_t offset, uint64_t value, int minDigits = 0);
   1.174 +    void insertHex(size_t offset, uint32_t value, int minDigits = 0);
   1.175 +    void insertScalar(size_t offset, SkScalar);
   1.176 +
   1.177 +    void append(const SkString& str) { this->insert((size_t)-1, str); }
   1.178 +    void append(const char text[]) { this->insert((size_t)-1, text); }
   1.179 +    void append(const char text[], size_t len) { this->insert((size_t)-1, text, len); }
   1.180 +    void appendUnichar(SkUnichar uni) { this->insertUnichar((size_t)-1, uni); }
   1.181 +    void appendS32(int32_t value) { this->insertS32((size_t)-1, value); }
   1.182 +    void appendS64(int64_t value, int minDigits = 0) { this->insertS64((size_t)-1, value, minDigits); }
   1.183 +    void appendU32(uint32_t value) { this->insertU32((size_t)-1, value); }
   1.184 +    void appendU64(uint64_t value, int minDigits = 0) { this->insertU64((size_t)-1, value, minDigits); }
   1.185 +    void appendHex(uint32_t value, int minDigits = 0) { this->insertHex((size_t)-1, value, minDigits); }
   1.186 +    void appendScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
   1.187 +
   1.188 +    void prepend(const SkString& str) { this->insert(0, str); }
   1.189 +    void prepend(const char text[]) { this->insert(0, text); }
   1.190 +    void prepend(const char text[], size_t len) { this->insert(0, text, len); }
   1.191 +    void prependUnichar(SkUnichar uni) { this->insertUnichar(0, uni); }
   1.192 +    void prependS32(int32_t value) { this->insertS32(0, value); }
   1.193 +    void prependS64(int32_t value, int minDigits = 0) { this->insertS64(0, value, minDigits); }
   1.194 +    void prependHex(uint32_t value, int minDigits = 0) { this->insertHex(0, value, minDigits); }
   1.195 +    void prependScalar(SkScalar value) { this->insertScalar((size_t)-1, value); }
   1.196 +
   1.197 +    void printf(const char format[], ...) SK_PRINTF_LIKE(2, 3);
   1.198 +    void appendf(const char format[], ...) SK_PRINTF_LIKE(2, 3);
   1.199 +    void appendVAList(const char format[], va_list);
   1.200 +    void prependf(const char format[], ...) SK_PRINTF_LIKE(2, 3);
   1.201 +
   1.202 +    void remove(size_t offset, size_t length);
   1.203 +
   1.204 +    SkString& operator+=(const SkString& s) { this->append(s); return *this; }
   1.205 +    SkString& operator+=(const char text[]) { this->append(text); return *this; }
   1.206 +    SkString& operator+=(const char c) { this->append(&c, 1); return *this; }
   1.207 +
   1.208 +    /**
   1.209 +     *  Swap contents between this and other. This function is guaranteed
   1.210 +     *  to never fail or throw.
   1.211 +     */
   1.212 +    void swap(SkString& other);
   1.213 +
   1.214 +private:
   1.215 +    struct Rec {
   1.216 +    public:
   1.217 +        uint32_t    fLength; // logically size_t, but we want it to stay 32bits
   1.218 +        int32_t     fRefCnt;
   1.219 +        char        fBeginningOfData;
   1.220 +
   1.221 +        char* data() { return &fBeginningOfData; }
   1.222 +        const char* data() const { return &fBeginningOfData; }
   1.223 +    };
   1.224 +    Rec* fRec;
   1.225 +
   1.226 +#ifdef SK_DEBUG
   1.227 +    const char* fStr;
   1.228 +    void validate() const;
   1.229 +#else
   1.230 +    void validate() const {}
   1.231 +#endif
   1.232 +
   1.233 +    static const Rec gEmptyRec;
   1.234 +    static Rec* AllocRec(const char text[], size_t len);
   1.235 +    static Rec* RefRec(Rec*);
   1.236 +};
   1.237 +
   1.238 +/// Creates a new string and writes into it using a printf()-style format.
   1.239 +SkString SkStringPrintf(const char* format, ...);
   1.240 +
   1.241 +// Specialized to take advantage of SkString's fast swap path. The unspecialized function is
   1.242 +// declared in SkTypes.h and called by SkTSort.
   1.243 +template <> inline void SkTSwap(SkString& a, SkString& b) {
   1.244 +    a.swap(b);
   1.245 +}
   1.246 +
   1.247 +// Split str on any characters in delimiters into out.  (Think, strtok with a sane API.)
   1.248 +void SkStrSplit(const char* str, const char* delimiters, SkTArray<SkString>* out);
   1.249 +
   1.250 +#endif

mercurial