/* * Copyright 2006 The Android Open Source Project * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #ifndef SkString_DEFINED #define SkString_DEFINED #include "include/core/SkRefCnt.h" #include "include/core/SkScalar.h" #include "include/core/SkTypes.h" #include "include/private/base/SkTo.h" #include "include/private/base/SkTypeTraits.h" #include <atomic> #include <cstdarg> #include <cstdint> #include <cstring> #include <string> #include <string_view> #include <type_traits> /* Some helper functions for C strings */ static inline bool SkStrStartsWith(const char string[], const char prefixStr[]) { … } static inline bool SkStrStartsWith(const char string[], const char prefixChar) { … } bool SkStrEndsWith(const char string[], const char suffixStr[]); bool SkStrEndsWith(const char string[], const char suffixChar); int SkStrStartsWithOneOf(const char string[], const char prefixes[]); static inline int SkStrFind(const char string[], const char substring[]) { … } static inline int SkStrFindLastOf(const char string[], const char subchar) { … } static inline bool SkStrContains(const char string[], const char substring[]) { … } static inline bool SkStrContains(const char string[], const char subchar) { … } /* * The SkStrAppend... methods will write into the provided buffer, assuming it is large enough. * Each method has an associated const (e.g. kSkStrAppendU32_MaxSize) which will be the largest * value needed for that method's buffer. * * char storage[kSkStrAppendU32_MaxSize]; * SkStrAppendU32(storage, value); * * Note : none of the SkStrAppend... methods write a terminating 0 to their buffers. Instead, * the methods return the ptr to the end of the written part of the buffer. This can be used * to compute the length, and/or know where to write a 0 if that is desired. * * char storage[kSkStrAppendU32_MaxSize + 1]; * char* stop = SkStrAppendU32(storage, value); * size_t len = stop - storage; * *stop = 0; // valid, since storage was 1 byte larger than the max. */ static constexpr int kSkStrAppendU32_MaxSize = …; char* SkStrAppendU32(char buffer[], uint32_t); static constexpr int kSkStrAppendU64_MaxSize = …; char* SkStrAppendU64(char buffer[], uint64_t, int minDigits); static constexpr int kSkStrAppendS32_MaxSize = …; char* SkStrAppendS32(char buffer[], int32_t); static constexpr int kSkStrAppendS64_MaxSize = …; char* SkStrAppendS64(char buffer[], int64_t, int minDigits); /** * Floats have at most 8 significant digits, so we limit our %g to that. * However, the total string could be 15 characters: -1.2345678e-005 * * In theory we should only expect up to 2 digits for the exponent, but on * some platforms we have seen 3 (as in the example above). */ static constexpr int kSkStrAppendScalar_MaxSize = …; /** * Write the scalar in decimal format into buffer, and return a pointer to * the next char after the last one written. Note: a terminating 0 is not * written into buffer, which must be at least kSkStrAppendScalar_MaxSize. * Thus if the caller wants to add a 0 at the end, buffer must be at least * kSkStrAppendScalar_MaxSize + 1 bytes large. */ char* SkStrAppendScalar(char buffer[], SkScalar); /** \class SkString Light weight class for managing strings. Uses reference counting to make string assignments and copies very fast with no extra RAM cost. Assumes UTF8 encoding. */ class SK_API SkString { … }; /// Creates a new string and writes into it using a printf()-style format. SK_API SkString SkStringPrintf(const char* format, ...) SK_PRINTF_LIKE(1, 2); /// This makes it easier to write a caller as a VAR_ARGS function where the format string is /// optional. static inline SkString SkStringPrintf() { … } static inline void swap(SkString& a, SkString& b) { … } #endif