diff options
author | Bryan O'Sullivan <bos@lindenlab.com> | 2009-06-22 15:02:19 -0700 |
---|---|---|
committer | Bryan O'Sullivan <bos@lindenlab.com> | 2009-06-22 15:02:19 -0700 |
commit | baa73fddd9287ddafd2d31551cb253b355ed910a (patch) | |
tree | e3f0986617fe6c0ee0a14df6aac13c6bb6f92507 /indra/llcommon/llstring.h | |
parent | dc3833f31b8a20220ddb1775e1625c016c397435 (diff) | |
parent | fcaa1ad46fd1df4cfec9dee12caf6e7b5bf32136 (diff) |
Merge with viewer-2.0.0-3 branch
Diffstat (limited to 'indra/llcommon/llstring.h')
-rw-r--r-- | indra/llcommon/llstring.h | 140 |
1 files changed, 88 insertions, 52 deletions
diff --git a/indra/llcommon/llstring.h b/indra/llcommon/llstring.h index da1900eadf..91b706a5be 100644 --- a/indra/llcommon/llstring.h +++ b/indra/llcommon/llstring.h @@ -147,7 +147,7 @@ struct char_traits<U16> }; #endif -class LL_COMMON_API LLStringOps +class LLStringOps { private: static long sltOffset; @@ -194,13 +194,13 @@ public: * @brief Return a string constructed from in without crashing if the * pointer is NULL. */ -LL_COMMON_API std::string ll_safe_string(const char* in); -LL_COMMON_API std::string ll_safe_string(const char* in, S32 maxlen); +std::string ll_safe_string(const char* in); +std::string ll_safe_string(const char* in, S32 maxlen); // Allowing assignments from non-strings into format_map_t is apparently // *really* error-prone, so subclass std::string with just basic c'tors. -class LL_COMMON_API LLFormatMapString +class LLFormatMapString { public: LLFormatMapString() {}; @@ -254,7 +254,25 @@ public: // True if this is the head of s. static BOOL isHead( const std::basic_string<T>& string, const T* s ); - + + /** + * @brief Returns true if string starts with substr + * + * If etither string or substr are empty, this method returns false. + */ + static bool startsWith( + const std::basic_string<T>& string, + const std::basic_string<T>& substr); + + /** + * @brief Returns true if string ends in substr + * + * If etither string or substr are empty, this method returns false. + */ + static bool endsWith( + const std::basic_string<T>& string, + const std::basic_string<T>& substr); + static void addCRLF(std::basic_string<T>& string); static void removeCRLF(std::basic_string<T>& string); @@ -362,7 +380,7 @@ public: * This function works on bytes rather than glyphs, so this will * incorrectly truncate non-single byte strings. * Use utf8str_truncate() for utf8 strings - * @return a copy of in string minus the trailing count characters. + * @return a copy of in string minus the trailing count bytes. */ inline std::string chop_tail_copy( const std::string& in, @@ -375,7 +393,7 @@ inline std::string chop_tail_copy( * @brief This translates a nybble stored as a hex value from 0-f back * to a nybble in the low order bits of the return byte. */ -LL_COMMON_API U8 hex_as_nybble(char hex); +U8 hex_as_nybble(char hex); /** * @brief read the contents of a file into a string. @@ -386,8 +404,8 @@ LL_COMMON_API U8 hex_as_nybble(char hex); * @param filename The full name of the file to read. * @return Returns true on success. If false, str is unmodified. */ -LL_COMMON_API bool _read_file_into_string(std::string& str, const std::string& filename); -LL_COMMON_API bool iswindividual(llwchar elem); +bool _read_file_into_string(std::string& str, const std::string& filename); +bool iswindividual(llwchar elem); /** * Unicode support @@ -396,52 +414,52 @@ LL_COMMON_API bool iswindividual(llwchar elem); // Make the incoming string a utf8 string. Replaces any unknown glyph // with the UNKOWN_CHARACTER. Once any unknown glph is found, the rest // of the data may not be recovered. -LL_COMMON_API std::string rawstr_to_utf8(const std::string& raw); +std::string rawstr_to_utf8(const std::string& raw); // // We should never use UTF16 except when communicating with Win32! // typedef std::basic_string<U16> llutf16string; -LL_COMMON_API LLWString utf16str_to_wstring(const llutf16string &utf16str, S32 len); -LL_COMMON_API LLWString utf16str_to_wstring(const llutf16string &utf16str); +LLWString utf16str_to_wstring(const llutf16string &utf16str, S32 len); +LLWString utf16str_to_wstring(const llutf16string &utf16str); -LL_COMMON_API llutf16string wstring_to_utf16str(const LLWString &utf32str, S32 len); -LL_COMMON_API llutf16string wstring_to_utf16str(const LLWString &utf32str); +llutf16string wstring_to_utf16str(const LLWString &utf32str, S32 len); +llutf16string wstring_to_utf16str(const LLWString &utf32str); -LL_COMMON_API llutf16string utf8str_to_utf16str ( const std::string& utf8str, S32 len); -LL_COMMON_API llutf16string utf8str_to_utf16str ( const std::string& utf8str ); +llutf16string utf8str_to_utf16str ( const std::string& utf8str, S32 len); +llutf16string utf8str_to_utf16str ( const std::string& utf8str ); -LL_COMMON_API LLWString utf8str_to_wstring(const std::string &utf8str, S32 len); -LL_COMMON_API LLWString utf8str_to_wstring(const std::string &utf8str); +LLWString utf8str_to_wstring(const std::string &utf8str, S32 len); +LLWString utf8str_to_wstring(const std::string &utf8str); // Same function, better name. JC inline LLWString utf8string_to_wstring(const std::string& utf8_string) { return utf8str_to_wstring(utf8_string); } // -LL_COMMON_API S32 wchar_to_utf8chars(llwchar inchar, char* outchars); +S32 wchar_to_utf8chars(llwchar inchar, char* outchars); -LL_COMMON_API std::string wstring_to_utf8str(const LLWString &utf32str, S32 len); -LL_COMMON_API std::string wstring_to_utf8str(const LLWString &utf32str); +std::string wstring_to_utf8str(const LLWString &utf32str, S32 len); +std::string wstring_to_utf8str(const LLWString &utf32str); -LL_COMMON_API std::string utf16str_to_utf8str(const llutf16string &utf16str, S32 len); -LL_COMMON_API std::string utf16str_to_utf8str(const llutf16string &utf16str); +std::string utf16str_to_utf8str(const llutf16string &utf16str, S32 len); +std::string utf16str_to_utf8str(const llutf16string &utf16str); // Length of this UTF32 string in bytes when transformed to UTF8 -LL_COMMON_API S32 wstring_utf8_length(const LLWString& wstr); +S32 wstring_utf8_length(const LLWString& wstr); // Length in bytes of this wide char in a UTF8 string -LL_COMMON_API S32 wchar_utf8_length(const llwchar wc); +S32 wchar_utf8_length(const llwchar wc); -LL_COMMON_API std::string utf8str_tolower(const std::string& utf8str); +std::string utf8str_tolower(const std::string& utf8str); // Length in llwchar (UTF-32) of the first len units (16 bits) of the given UTF-16 string. -LL_COMMON_API S32 utf16str_wstring_length(const llutf16string &utf16str, S32 len); +S32 utf16str_wstring_length(const llutf16string &utf16str, S32 len); // Length in utf16string (UTF-16) of wlen wchars beginning at woffset. -LL_COMMON_API S32 wstring_utf16_length(const LLWString & wstr, S32 woffset, S32 wlen); +S32 wstring_utf16_length(const LLWString & wstr, S32 woffset, S32 wlen); // Length in wstring (i.e., llwchar count) of a part of a wstring specified by utf16 length (i.e., utf16 units.) -LL_COMMON_API S32 wstring_wstring_length_from_utf16_length(const LLWString & wstr, S32 woffset, S32 utf16_length, BOOL *unaligned = NULL); +S32 wstring_wstring_length_from_utf16_length(const LLWString & wstr, S32 woffset, S32 utf16_length, BOOL *unaligned = NULL); /** * @brief Properly truncate a utf8 string to a maximum byte count. @@ -453,11 +471,11 @@ LL_COMMON_API S32 wstring_wstring_length_from_utf16_length(const LLWString & wst * @param max_len The maximum number of bytes in the return value. * @return Returns a valid utf8 string with byte count <= max_len. */ -LL_COMMON_API std::string utf8str_truncate(const std::string& utf8str, const S32 max_len); +std::string utf8str_truncate(const std::string& utf8str, const S32 max_len); -LL_COMMON_API std::string utf8str_trim(const std::string& utf8str); +std::string utf8str_trim(const std::string& utf8str); -LL_COMMON_API S32 utf8str_compare_insensitive( +S32 utf8str_compare_insensitive( const std::string& lhs, const std::string& rhs); @@ -468,17 +486,17 @@ LL_COMMON_API S32 utf8str_compare_insensitive( * @param target_char The wchar to be replaced * @param replace_char The wchar which is written on replace */ -LL_COMMON_API std::string utf8str_substChar( +std::string utf8str_substChar( const std::string& utf8str, const llwchar target_char, const llwchar replace_char); -LL_COMMON_API std::string utf8str_makeASCII(const std::string& utf8str); +std::string utf8str_makeASCII(const std::string& utf8str); // Hack - used for evil notecards. -LL_COMMON_API std::string mbcsstring_makeASCII(const std::string& str); +std::string mbcsstring_makeASCII(const std::string& str); -LL_COMMON_API std::string utf8str_removeCRLF(const std::string& utf8str); +std::string utf8str_removeCRLF(const std::string& utf8str); #if LL_WINDOWS @@ -503,21 +521,14 @@ LL_COMMON_API std::string utf8str_removeCRLF(const std::string& utf8str); * formatted string. * */ - -// Deal with the differeneces on Windows -namespace snprintf_hack -{ - LL_COMMON_API int snprintf(char *str, size_t size, const char *format, ...); -} - -using snprintf_hack::snprintf; +int safe_snprintf(char* str, size_t size, const char* format, ...); /** * @brief Convert a wide string to std::string * * This replaces the unsafe W2A macro from ATL. */ -LL_COMMON_API std::string ll_convert_wide_to_string(const wchar_t* in); +std::string ll_convert_wide_to_string(const wchar_t* in); //@} #endif // LL_WINDOWS @@ -540,7 +551,7 @@ namespace LLStringFn * with zero non-printable characters. * @param The replacement character. use LL_UNKNOWN_CHAR if unsure. */ - LL_COMMON_API void replace_nonprintable_in_ascii( + void replace_nonprintable_in_ascii( std::basic_string<char>& string, char replacement); @@ -554,7 +565,7 @@ namespace LLStringFn * with zero non-printable characters and zero pipe characters. * @param The replacement character. use LL_UNKNOWN_CHAR if unsure. */ - LL_COMMON_API void replace_nonprintable_and_pipe_in_ascii(std::basic_string<char>& str, + void replace_nonprintable_and_pipe_in_ascii(std::basic_string<char>& str, char replacement); @@ -563,7 +574,7 @@ namespace LLStringFn * Returns a copy of the string with those characters removed. * Works with US ASCII and UTF-8 encoded strings. JC */ - LL_COMMON_API std::string strip_invalid_xml(const std::string& input); + std::string strip_invalid_xml(const std::string& input); /** @@ -574,7 +585,7 @@ namespace LLStringFn * with zero non-printable characters. * @param The replacement character. use LL_UNKNOWN_CHAR if unsure. */ - LL_COMMON_API void replace_ascii_controlchars( + void replace_ascii_controlchars( std::basic_string<char>& string, char replacement); } @@ -757,15 +768,16 @@ bool LLStringUtilBase<T>::formatDatetime(std::basic_string<T>& replacement, std: { S32 secFromEpoch = (long) substitutions["datetime"].asInteger(); - if (param == "local") + if (param == "local") // local { secFromEpoch -= LLStringOps::getLocalTimeOffset(); } - else if (param != "utc") + else if (param != "utc") // slt { secFromEpoch -= LLStringOps::getSltOffset(); } - + + // if never fell into those two ifs above, param must be utc if (secFromEpoch < 0) secFromEpoch = 0; LLDate * datetime = new LLDate((F64)secFromEpoch); @@ -1251,6 +1263,30 @@ BOOL LLStringUtilBase<T>::isHead( const std::basic_string<T>& string, const T* s } } +// static +template<class T> +bool LLStringUtilBase<T>::startsWith( + const std::basic_string<T>& string, + const std::basic_string<T>& substr) +{ + if(string.empty() || (substr.empty())) return false; + if(0 == string.find(substr)) return true; + return false; +} + +// static +template<class T> +bool LLStringUtilBase<T>::endsWith( + const std::basic_string<T>& string, + const std::basic_string<T>& substr) +{ + if(string.empty() || (substr.empty())) return false; + std::string::size_type idx = string.rfind(substr); + if(std::string::npos == idx) return false; + return (idx == (string.size() - substr.size())); +} + + template<class T> BOOL LLStringUtilBase<T>::convertToBOOL(const std::basic_string<T>& string, BOOL& value) { |