📄 string.hh
字号:
// -*- related-file-name: "../../lib/string.cc" -*-#ifndef CLICK_STRING_HH#define CLICK_STRING_HH#include <click/algorithm.hh>#include <click/atomic.hh>CLICK_DECLSclass String { public: /** @brief Initialize the String implementation. * @deprecated Initializing or cleaning up the String implementation is * no longer necessary. * * In previous versions, this function needed to be called before any * String functionality was used. It currently does nothing. */ static void static_initialize() CLICK_DEPRECATED; /** @brief Clean up the String implementation. * @deprecated Initializing or cleaning up the String implementation is * no longer necessary. * * In previous versions, this function was called to release memory * allocated by the String implementation. It currently does nothing. */ static void static_cleanup() CLICK_DEPRECATED; /** @class String::Initializer * @brief Initializes the String implementation. * @deprecated Initializing or cleaning up the String implementation is * no longer necessary. * * In previous versions, this class's constructor initialized the String * implementation. It currently does nothing. */ struct Initializer { Initializer() CLICK_DEPRECATED; }; /** @brief Construct an empty String (with length 0). */ inline String() { _r.data = null_memo.real_data; _r.length = 0; _r.memo = &null_memo; atomic_uint32_t::inc(_r.memo->refcount); } /** @brief Construct a copy of the String @a x. */ inline String(const String &x) { assign(x); } /** @brief Construct a String containing the C string @a cstr. * @param cstr a null-terminated C string * @return A String containing the characters of @a cstr, up to but not * including the terminating null character. * * If @a cstr equals String::out_of_memory_data(), returns an * out-of-memory string. */ inline String(const char *cstr) { assign(cstr, -1, false); } /** @brief Construct a String containing the first @a len characters of * string @a s. * @param s a string * @param len number of characters to take from @a s. If @a len @< 0, * then takes @c strlen(@a s) characters. * @return A String containing @a len characters of @a s. * * If @a s equals String::out_of_memory_data(), returns an out-of-memory * string. */ inline String(const char *s, int len) { assign(s, len, false); } /** @brief Construct a String containing the characters from @a begin * to @a end. * @param begin first character in string (begin iterator) * @param end pointer one past last character in string (end iterator) * @return A String containing the characters from @a begin to @a end. * * Returns a null string if @a begin @>= @a end. If @a begin equals * String::out_of_memory_data(), returns an out-of-memory string. */ inline String(const char *begin, const char *end) { assign(begin, (end > begin ? end - begin : 0), false); } /** @brief Construct a String equal to "true" or "false" depending on the * value of @a b. */ explicit inline String(bool b) { _r.data = bool_data + (b ? 0 : 5); _r.length = (b ? 4 : 5); _r.memo = &permanent_memo; atomic_uint32_t::inc(_r.memo->refcount); } /** @brief Construct a String containing the single character @a c. */ explicit inline String(char c) { assign(&c, 1, false); } /** @overload */ explicit inline String(unsigned char c) { assign(reinterpret_cast<char *>(&c), 1, false); } /** @brief Construct a base-10 string representation of @a x. */ explicit String(int x); /** @overload */ explicit String(unsigned x); /** @overload */ explicit String(long x); /** @overload */ explicit String(unsigned long x);#if HAVE_LONG_LONG /** @overload */ explicit String(long long x); /** @overload */ explicit String(unsigned long long x);#endif#if HAVE_INT64_TYPES && !HAVE_INT64_IS_LONG && !HAVE_INT64_IS_LONG_LONG /** @overload */ explicit String(int64_t x); /** @overload */ explicit String(uint64_t x);#endif#if HAVE_FLOAT_TYPES /** @brief Construct a base-10 string representation of @a x. * @note This function is only available at user level. */ explicit String(double x);#endif /** @brief Destroy a String, freeing memory if necessary. */ inline ~String() { deref(); } /** @brief Return a const reference to an empty String. * * May be quicker than String::String(). */ static inline const String &make_empty() { return reinterpret_cast<const String &>(null_string_rep); } /** @brief Return a String containing @a len unknown characters. */ static String make_garbage(int len); /** @brief Return a String that directly references the first @a len * characters of @a s. * * This function is suitable for static constant strings whose data is * known to stay around forever, such as C string constants. If @a len @< * 0, treats @a s as a null-terminated C string. */ static String make_stable(const char *s, int len = -1); /** @brief Return a String that directly references the character data in * [@a begin, @a end). * @param begin pointer to the first character in the character data * @param end pointer one beyond the last character in the character data * * This function is suitable for static constant strings whose data is * known to stay around forever, such as C string constants. Returns an * empty string if @a begin @>= @a end. */ static inline String make_stable(const char *begin, const char *end) { if (begin < end) return String::make_stable(begin, end - begin); else return String(); }#if HAVE_INT64_TYPES && (!HAVE_LONG_LONG || SIZEOF_LONG_LONG <= 8) typedef int64_t int_large_t; typedef uint64_t uint_large_t;#elif HAVE_LONG_LONG typedef long long int_large_t; typedef unsigned long long uint_large_t;#else typedef long int_large_t; typedef unsigned long uint_large_t;#endif /** @brief Create and return a string representation of @a x. * @param x number * @param base base; must be 8, 10, or 16, defaults to 10 * @param uppercase if true, then use uppercase letters in base 16 */ static String make_numeric(int_large_t x, int base = 10, bool uppercase = true); /** @overload */ static String make_numeric(uint_large_t x, int base = 10, bool uppercase = true); static inline const String &empty_string() CLICK_DEPRECATED; static inline String garbage_string(int len) CLICK_DEPRECATED; static inline String stable_string(const char *s, int len = -1) CLICK_DEPRECATED; static inline String stable_string(const char *begin, const char *end) CLICK_DEPRECATED; static inline String numeric_string(int_large_t x, int base = 10, bool uppercase = true) CLICK_DEPRECATED; static inline String numeric_string(uint_large_t x, int base = 10, bool uppercase = true) CLICK_DEPRECATED; /** @brief Return the string's length. */ inline int length() const { return _r.length; } /** @brief Return a pointer to the string's data. * * Only the first length() characters are valid, and the string data * might not be null-terminated. */ inline const char *data() const { return _r.data; } typedef const char *const_iterator; typedef const_iterator iterator; /** @brief Return an iterator for the first character in the string. * * String iterators are simply pointers into string data, so they are * quite efficient. @sa String::data */ inline const_iterator begin() const { return _r.data; } /** @brief Return an iterator for the end of the string. * * The return value points one character beyond the last character in the * string. */ inline const_iterator end() const { return _r.data + _r.length; } typedef int (String::*unspecified_bool_type)() const; /** @brief Return true iff the string is nonempty. */ inline operator unspecified_bool_type() const { return _r.length != 0 ? &String::length : 0; } /** @brief Return true iff the string is empty. */ inline bool operator!() const { return _r.length == 0; } /** @brief Return the @a i th character in the string. * * Does not check bounds. @sa String::at */ inline char operator[](int i) const { return _r.data[i]; } /** @brief Return the @a i th character in the string. * * Checks bounds: an assertion will fail if @a i is less than 0 or not * less than length(). @sa String::operator[] */ inline char at(int i) const { assert((unsigned) i < (unsigned) _r.length); return _r.data[i]; } /** @brief Return the first character in the string. * * Does not check bounds. Same as (*this)[0]. */ inline char front() const { return _r.data[0]; } /** @brief Return the last character in the string. * * Does not check bounds. Same as (*this)[length() - 1]. */ inline char back() const { return _r.data[_r.length - 1]; } /** @brief Null-terminate the string. * * The terminating null character isn't considered part of the string, so * this->length() doesn't change. Returns a corresponding C string * pointer. The returned pointer is semi-temporary; it will persist until * the string is destroyed or appended to. */ const char *c_str() const; /** @brief Return a 32-bit hash function of the characters in [begin, end). * * Uses Paul Hsieh's "SuperFastHash" algorithm, described at * http://www.azillionmonkeys.com/qed/hash.html * This hash function uses all characters in the string. * * @invariant If end1 - begin1 == end2 - begin2 and memcmp(begin1, begin2, * end1 - begin1) == 0, then hashcode(begin1, end1) == hashcode(begin2, * end2). */ static uint32_t hashcode(const char *begin, const char *end); /** @overload */ static inline uint32_t hashcode(const unsigned char *begin, const unsigned char *end) { return hashcode(reinterpret_cast<const char *>(begin), reinterpret_cast<const char *>(end)); } /** @brief Returns a 32-bit hash function of this string's characters. * * Equivalent to String::hashcode(begin(), end()). Uses Paul Hsieh's * "SuperFastHash." * * @invariant If s1 == s2, then s1.hashcode() == s2.hashcode(). */ inline uint32_t hashcode() const { return length() ? hashcode(begin(), end()) : 0; } /** @brief Return true iff this string is equal to the data in @a s. * @param s string data to compare to * @param len length of @a s * * Same as String::compare(*this, String(s, len)) == 0. If @a len @< 0, * then treats @a s as a null-terminated C string. * * @sa String::compare(const String &a, const String &b) */ bool equals(const char *s, int len) const; // bool operator==(const String &, const String &); // bool operator==(const String &, const char *); // bool operator==(const char *, const String &); // bool operator!=(const String &, const String &); // bool operator!=(const String &, const char *); // bool operator!=(const char *, const String &); /** @brief Compare two strings. * @param a first string to compare * @param b second string to compare * * Returns 0 if @a a == @a b, negative if @a a @< @a b in lexicographic * order, and positive if @a a @> @a b in lexicographic order. The * lexicographic order treats all characters as unsigned. */ static inline int compare(const String &a, const String &b) { return a.compare(b); } /** @brief Compare this string with string @a x. * * Same as String::compare(*this, @a x). * @sa String::compare(const String &a, const String &b) */ inline int compare(const String &x) const { return compare(x._r.data, x._r.length); } /** @brief Compare this string with the data in @a s. * @param s string data to compare to * @param len length of @a s * * Same as String::compare(*this, String(s, len)). If @a len @< 0, then * treats @a s as a null-terminated C string. * * @sa String::compare(const String &a, const String &b) */ int compare(const char *s, int len) const; // bool operator<(const String &, const String &); // bool operator<=(const String &, const String &); // bool operator>(const String &, const String &); // bool operator>=(const String &, const String &); /** @brief Return a substring of the current string starting at @a begin * and ending before @a end. * @param begin pointer to the first substring character * @param end pointer one beyond the last substring character * * Returns an empty string if @a begin @>= @a end. Also returns an empty * string if @a begin or @a end is out of range (i.e., either less than * this->begin() or greater than this->end()), but this should be * considered a programming error; a future version may generate a warning * for this case. */ inline String substring(const char *begin, const char *end) const { if (begin < end && begin >= _r.data && end <= _r.data + _r.length) return String(begin, end - begin, _r.memo); else return String(); } /** @brief Return a substring of this string, consisting of the @a len * characters starting at index @a pos. * @param pos substring's first position relative to the string * @param len length of substring * * If @a pos is negative, starts that far from the end of the string. If * @a len is negative, leaves that many characters off the end of the * string. If @a pos and @a len specify a substring that is partly * outside the string, only the part within the string is returned. If * the substring is beyond either end of the string, returns an empty * string (but this should be considered a programming error; a future * version may generate a warning for this case). * * @note String::substring() is intended to behave like Perl's substr(). */ String substring(int pos, int len) const; /** @brief Return the suffix of the current string starting at index @a pos. * * If @a pos is negative, starts that far from the end of the string. If * @a pos is so negative that the suffix starts outside the string, then * the entire string is returned. If the substring is beyond the end of * the string (@a pos > length()), returns an empty string (but this * should be considered a programming error; a future version may generate * a warning for this case). * * @note String::substring() is intended to behave like Perl's substr(). */ inline String substring(int pos) const { return substring((pos <= -_r.length ? 0 : pos), _r.length); } /** @brief Search for a character in a string. * @param c character to search for * @param start initial search position *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -