Rapicorn - Experimental UI Toolkit - Source Code
13.07.0
|
00001 // Licensed GNU LGPL v3 or later: http://www.gnu.org/licenses/lgpl.html 00002 #ifndef __RAPICORN_STRINGS_HH__ 00003 #define __RAPICORN_STRINGS_HH__ 00004 00005 #include <rcore/formatter.hh> 00006 #include <string> 00007 #include <cstring> 00008 00009 namespace Rapicorn { 00010 00011 // == i18n == 00012 const char* rapicorn_gettext (const char *text) RAPICORN_FORMAT (1); 00013 #ifdef __RAPICORN_BUILD__ 00014 #define _(str) Rapicorn::rapicorn_gettext (str) 00015 #define N_(str) (str) 00016 #endif 00017 00018 // == Macros == 00019 #ifdef RAPICORN_CONVENIENCE 00020 00021 #define CQUOTE(str) RAPICORN_CQUOTE(str) 00022 00023 #define STRING_VECTOR_FROM_ARRAY(ConstCharArray) RAPICORN_STRING_VECTOR_FROM_ARRAY(ConstCharArray) 00024 #endif // RAPICORN_CONVENIENCE 00025 00026 // == C-String == 00027 bool cstring_to_bool (const char *string, bool fallback = false); 00028 00029 // == String Formatting == 00030 template<class... Args> String string_format (const char *format, const Args &...args) RAPICORN_PRINTF (1, 0); 00031 template<class... Args> String string_locale_format (const char *format, const Args &...args) RAPICORN_PRINTF (1, 0); 00032 String string_vprintf (const char *format, va_list vargs); 00033 String string_locale_vprintf (const char *format, va_list vargs); 00034 00035 // == String == 00036 String string_multiply (const String &s, uint64 count); 00037 String string_canonify (const String &s, const String &valid_chars, const String &substitute); 00038 String string_set_a2z (); 00039 String string_set_A2Z (); 00040 String string_set_ascii_alnum (); 00041 String string_tolower (const String &str); 00042 String string_toupper (const String &str); 00043 String string_totitle (const String &str); 00044 StringVector string_split (const String &string, const String &splitter = ""); 00045 String string_join (const String &junctor, const StringVector &strvec); 00046 bool string_to_bool (const String &string, bool fallback = false); 00047 String string_from_bool (bool value); 00048 uint64 string_to_uint (const String &string, uint base = 10); 00049 String string_from_uint (uint64 value); 00050 bool string_has_int (const String &string); 00051 int64 string_to_int (const String &string, uint base = 10); 00052 String string_from_int (int64 value); 00053 String string_from_float (float value); 00054 double string_to_double (const String &string); 00055 double string_to_double (const char *dblstring, const char **endptr); 00056 String string_from_double (double value); 00057 inline String string_from_float (double value) { return string_from_double (value); } 00058 inline double string_to_float (const String &string) { return string_to_double (string); } 00059 template<typename Type> Type string_to_type (const String &string); 00060 template<typename Type> String string_from_type (Type value); 00061 template<> inline double string_to_type<double> (const String &string) { return string_to_double (string); } 00062 template<> inline String string_from_type<double> (double value) { return string_from_double (value); } 00063 template<> inline float string_to_type<float> (const String &string) { return string_to_float (string); } 00064 template<> inline String string_from_type<float> (float value) { return string_from_float (value); } 00065 template<> inline bool string_to_type<bool> (const String &string) { return string_to_bool (string); } 00066 template<> inline String string_from_type<bool> (bool value) { return string_from_bool (value); } 00067 template<> inline int16 string_to_type<int16> (const String &string) { return string_to_int (string); } 00068 template<> inline String string_from_type<int16> (int16 value) { return string_from_int (value); } 00069 template<> inline uint16 string_to_type<uint16> (const String &string) { return string_to_uint (string); } 00070 template<> inline String string_from_type<uint16> (uint16 value) { return string_from_uint (value); } 00071 template<> inline int string_to_type<int> (const String &string) { return string_to_int (string); } 00072 template<> inline String string_from_type<int> (int value) { return string_from_int (value); } 00073 template<> inline uint string_to_type<uint> (const String &string) { return string_to_uint (string); } 00074 template<> inline String string_from_type<uint> (uint value) { return string_from_uint (value); } 00075 template<> inline int64 string_to_type<int64> (const String &string) { return string_to_int (string); } 00076 template<> inline String string_from_type<int64> (int64 value) { return string_from_int (value); } 00077 template<> inline uint64 string_to_type<uint64> (const String &string) { return string_to_uint (string); } 00078 template<> inline String string_from_type<uint64> (uint64 value) { return string_from_uint (value); } 00079 template<> inline String string_to_type<String> (const String &string) { return string; } 00080 template<> inline String string_from_type<String> (String value) { return value; } 00081 vector<double> string_to_double_vector (const String &string); 00082 String string_from_double_vector(const vector<double> &dvec, 00083 const String &delim = " "); 00084 String string_from_errno (int errno_val); 00085 bool string_is_uuid (const String &uuid_string); /* check uuid formatting */ 00086 int string_cmp_uuid (const String &uuid_string1, 00087 const String &uuid_string2); /* -1=smaller, 0=equal, +1=greater (assuming valid uuid strings) */ 00088 bool string_startswith (const String &string, const String &fragment); 00089 bool string_endswith (const String &string, const String &fragment); 00090 bool string_match_identifier (const String &ident1, const String &ident2); 00091 bool string_match_identifier_tail (const String &ident, const String &tail); 00092 String string_from_pretty_function_name (const char *gnuc_pretty_function); 00093 String string_to_cescape (const String &str); 00094 String string_to_cquote (const String &str); 00095 String string_from_cquote (const String &input); 00096 String string_hexdump (const void *addr, size_t length, size_t initial_offset = 0); 00097 String string_lstrip (const String &input); 00098 String string_rstrip (const String &input); 00099 String string_strip (const String &input); 00100 String string_substitute_char (const String &input, const char match, const char subst); 00101 String string_vector_find (const StringVector &svector, const String &key, const String &fallback); 00102 StringVector cstrings_to_vector (const char*, ...) RAPICORN_SENTINEL; 00103 void memset4 (uint32 *mem, uint32 filler, uint length); 00104 long double posix_locale_strtold (const char *nptr, char **endptr); 00105 long double current_locale_strtold (const char *nptr, char **endptr); 00106 00107 // == String Options == 00108 bool string_option_check (const String &option_string, 00109 const String &option); 00110 String string_option_get (const String &option_string, 00111 const String &option); 00112 void string_options_split (const String &option_string, 00113 vector<String> &option_names, 00114 vector<String> &option_values, 00115 const String &empty_default = ""); 00116 00117 // == Strings == 00119 class Strings : public std::vector<std::string> 00120 { 00121 typedef const std::string CS; 00122 public: 00123 explicit Strings (CS &s1); 00124 explicit Strings (CS &s1, CS &s2); 00125 explicit Strings (CS &s1, CS &s2, CS &s3); 00126 explicit Strings (CS &s1, CS &s2, CS &s3, CS &s4); 00127 explicit Strings (CS &s1, CS &s2, CS &s3, CS &s4, CS &s5); 00128 explicit Strings (CS &s1, CS &s2, CS &s3, CS &s4, CS &s5, CS &s6); 00129 explicit Strings (CS &s1, CS &s2, CS &s3, CS &s4, CS &s5, CS &s6, CS &s7); 00130 explicit Strings (CS &s1, CS &s2, CS &s3, CS &s4, CS &s5, CS &s6, CS &s7, CS &s8); 00131 explicit Strings (CS &s1, CS &s2, CS &s3, CS &s4, CS &s5, CS &s6, CS &s7, CS &s8, CS &s9); 00132 explicit Strings (CS &s1, CS &s2, CS &s3, CS &s4, CS &s5, CS &s6, CS &s7, CS &s8, CS &s9, CS &sA); 00133 explicit Strings (CS &s1, CS &s2, CS &s3, CS &s4, CS &s5, CS &s6, CS &s7, CS &s8, CS &s9, CS &sA, CS &sB); 00134 explicit Strings (CS &s1, CS &s2, CS &s3, CS &s4, CS &s5, CS &s6, CS &s7, CS &s8, CS &s9, CS &sA, CS &sB, CS &sC); 00135 }; 00136 00137 // == Charset Conversions == 00138 bool text_convert (const String &to_charset, 00139 String &output_string, 00140 const String &from_charset, 00141 const String &input_string, 00142 const String &fallback_charset = "ISO-8859-15", 00143 const String &output_mark = ""); 00144 00145 // == C strings == 00146 using ::strerror; // introduce (const char* strerror (int)) 00147 const char* strerror (); // simple wrapper for strerror (errno) 00148 00149 // == Implementations == 00150 #define RAPICORN_STRING_VECTOR_FROM_ARRAY(ConstCharArray) ({ \ 00151 Rapicorn::StringVector __a; \ 00152 const Rapicorn::uint64 __l = RAPICORN_ARRAY_SIZE (ConstCharArray); \ 00153 for (Rapicorn::uint64 __ai = 0; __ai < __l; __ai++) \ 00154 __a.push_back (ConstCharArray[__ai]); \ 00155 __a; }) 00156 #define RAPICORN_CQUOTE(str) (Rapicorn::string_to_cquote (str).c_str()) 00157 00159 template<class... Args> RAPICORN_NOINLINE String 00160 string_format (const char *format, const Args &...args) 00161 { 00162 return Lib::StringFormatter::format (NULL, format, args...); 00163 } 00164 00166 template<class... Args> RAPICORN_NOINLINE String 00167 string_locale_format (const char *format, const Args &...args) 00168 { 00169 return Lib::StringFormatter::format<Lib::StringFormatter::CURRENT_LOCALE> (NULL, format, args...); 00170 } 00171 00172 } // Rapicorn 00173 00174 #endif /* __RAPICORN_STRINGS_HH__ */