1 #ifndef __STRINGS_HPP__ 2 #define __STRINGS_HPP__ 12 #define LITERAL_ITEM_SC( ty, name, pref, content ) \ 13 static constexpr ty const *name##Str = pref##content; \ 14 static constexpr ty const name##Char = name##Str[0]; 16 #define LITERAL_ITEM_S( ty, name, pref, content ) \ 17 static constexpr ty const *name##Str = pref##content; 19 #define LITERAL_ITEM_LIST( ty, pref ) \ 20 LITERAL_ITEM_SC(ty, A, pref, "A") \ 21 LITERAL_ITEM_SC(ty, B, pref, "B") \ 22 LITERAL_ITEM_SC(ty, C, pref, "C") \ 23 LITERAL_ITEM_SC(ty, D, pref, "D") \ 24 LITERAL_ITEM_SC(ty, E, pref, "E") \ 25 LITERAL_ITEM_SC(ty, F, pref, "F") \ 26 LITERAL_ITEM_SC(ty, X, pref, "X") \ 27 LITERAL_ITEM_SC(ty, Z, pref, "Z") \ 28 LITERAL_ITEM_SC(ty, a, pref, "a") \ 29 LITERAL_ITEM_SC(ty, b, pref, "b") \ 30 LITERAL_ITEM_SC(ty, c, pref, "c") \ 31 LITERAL_ITEM_SC(ty, d, pref, "d") \ 32 LITERAL_ITEM_SC(ty, e, pref, "e") \ 33 LITERAL_ITEM_SC(ty, f, pref, "f") \ 34 LITERAL_ITEM_SC(ty, n, pref, "n") \ 35 LITERAL_ITEM_SC(ty, r, pref, "r") \ 36 LITERAL_ITEM_SC(ty, t, pref, "t") \ 37 LITERAL_ITEM_SC(ty, v, pref, "v") \ 38 LITERAL_ITEM_SC(ty, x, pref, "x") \ 39 LITERAL_ITEM_SC(ty, z, pref, "z") \ 40 LITERAL_ITEM_SC(ty, zero, pref, "0") \ 41 LITERAL_ITEM_SC(ty, one, pref, "1") \ 42 LITERAL_ITEM_SC(ty, two, pref, "2") \ 43 LITERAL_ITEM_SC(ty, three, pref, "3") \ 44 LITERAL_ITEM_SC(ty, four, pref, "4") \ 45 LITERAL_ITEM_SC(ty, five, pref, "5") \ 46 LITERAL_ITEM_SC(ty, six, pref, "6") \ 47 LITERAL_ITEM_SC(ty, seven, pref, "7") \ 48 LITERAL_ITEM_SC(ty, eight, pref, "8") \ 49 LITERAL_ITEM_SC(ty, nine, pref, "9") \ 50 LITERAL_ITEM_SC(ty, nul, pref, "\0") \ 51 LITERAL_ITEM_SC(ty, bel, pref, "\a") \ 52 LITERAL_ITEM_SC(ty, bs, pref, "\b") \ 53 LITERAL_ITEM_SC(ty, ht, pref, "\t") \ 54 LITERAL_ITEM_SC(ty, lf, pref, "\n") \ 55 LITERAL_ITEM_SC(ty, vt, pref, "\v") \ 56 LITERAL_ITEM_SC(ty, ff, pref, "\f") \ 57 LITERAL_ITEM_SC(ty, cr, pref, "\r") \ 58 LITERAL_ITEM_S(ty, empty, pref, "") \ 59 LITERAL_ITEM_SC(ty, space, pref, " ") \ 60 LITERAL_ITEM_SC(ty, under, pref, "_") \ 61 LITERAL_ITEM_SC(ty, dollar, pref, "$") \ 62 LITERAL_ITEM_SC(ty, slash, pref, "\\") \ 63 LITERAL_ITEM_SC(ty, divide, pref, "/") \ 64 LITERAL_ITEM_SC(ty, positive, pref, "+") \ 65 LITERAL_ITEM_SC(ty, negative, pref, "-") \ 66 LITERAL_ITEM_SC(ty, quote, pref, "\"") \ 67 LITERAL_ITEM_SC(ty, apos, pref, "\'") \ 68 LITERAL_ITEM_SC(ty, sharp, pref, "#") \ 69 LITERAL_ITEM_SC(ty, caret, pref, "^") \ 70 LITERAL_ITEM_SC(ty, pipe, pref, "|") \ 71 LITERAL_ITEM_SC(ty, amp, pref, "&") \ 72 LITERAL_ITEM_SC(ty, lt, pref, "<") \ 73 LITERAL_ITEM_SC(ty, gt, pref, ">") \ 74 LITERAL_ITEM_SC(ty, dbldivide, pref, "//") \ 75 LITERAL_ITEM_S(ty, crlf, pref, "\r\n") \ 76 LITERAL_ITEM_S(ty, slash_a, pref, "\\a") \ 77 LITERAL_ITEM_S(ty, slash_b, pref, "\\b") \ 78 LITERAL_ITEM_S(ty, slash_t, pref, "\\t") \ 79 LITERAL_ITEM_S(ty, slash_n, pref, "\\n") \ 80 LITERAL_ITEM_S(ty, slash_v, pref, "\\v") \ 81 LITERAL_ITEM_S(ty, slash_f, pref, "\\f") \ 82 LITERAL_ITEM_S(ty, slash_r, pref, "\\r") \ 83 LITERAL_ITEM_S(ty, slash_x, pref, "\\x") \ 84 LITERAL_ITEM_S(ty, cslashes, pref, "\n\r\t\v\a\\\'\"") 86 template <
typename _ChTy >
127 #if defined(_UNICODE) || defined(UNICODE) 144 #if defined(_UNICODE) || defined(UNICODE) 154 #if defined(_UNICODE) || defined(UNICODE) 164 #if defined(_UNICODE) || defined(UNICODE) 174 #if defined(_UNICODE) || defined(UNICODE) 184 #if defined(_UNICODE) || defined(UNICODE) 197 #if defined(_UNICODE) || defined(UNICODE) 207 #if defined(_UNICODE) || defined(UNICODE) 216 #if defined(_UNICODE) || defined(UNICODE) 226 #if defined(_UNICODE) || defined(UNICODE) 235 #if defined(_UNICODE) || defined(UNICODE) 245 #if defined(_UNICODE) || defined(UNICODE) 255 #if defined(_UNICODE) || defined(UNICODE) 274 #if defined(_UNICODE) || defined(UNICODE) 277 inline uint64 StrToXq(
char const * nptr,
char const ** endptr,
int ibase,
int flags ) {
return StrToXqA( nptr, endptr, ibase, flags ); }
286 #if defined(_UNICODE) || defined(UNICODE) 295 #if defined(_UNICODE) || defined(UNICODE) 305 #if defined(_UNICODE) || defined(UNICODE) 315 #if defined(_UNICODE) || defined(UNICODE) 325 #if defined(_UNICODE) || defined(UNICODE) 335 #if defined(_UNICODE) || defined(UNICODE) 345 #if defined(_UNICODE) || defined(UNICODE) 362 #if defined(_UNICODE) || defined(UNICODE) 387 #if defined(_UNICODE) || defined(UNICODE) 397 #if defined(_UNICODE) || defined(UNICODE) 405 template <
typename _IndexType,
typename _ChTy >
408 std::vector<_IndexType> next( sublen + 1 );
413 if ( k == -1 || substr[j] == substr[k] )
418 next[j] = (_IndexType)k;
431 template <
typename _IndexType,
typename _ChTy >
432 inline static size_t _Templ_KmpMatchEx( _ChTy
const * str,
size_t len, _ChTy
const * substr,
size_t sublen,
size_t pos, std::vector<_IndexType>
const & next )
439 if ( str[i] == substr[j] )
459 template <
typename _IndexType,
typename _ChTy >
460 inline static size_t _Templ_KmpMatch( _ChTy
const * str,
size_t len, _ChTy
const * substr,
size_t sublen,
size_t pos )
462 return _Templ_KmpMatchEx<_IndexType, _ChTy>( str, len, substr, sublen, pos,
_Templ_KmpCalcNext( substr, sublen ) );
468 WINUX_FUNC_DECL(
size_t)
KmpMatchEx(
char const * str,
size_t len,
char const * substr,
size_t sublen,
size_t pos, std::vector<int>
const & next );
470 WINUX_FUNC_DECL(
size_t)
KmpMatch(
char const * str,
size_t len,
char const * substr,
size_t sublen,
size_t pos );
474 template <
typename _ChTy >
478 std::basic_ostringstream<_ChTy> * _sout;
487 _sout =
new std::basic_ostringstream<_ChTy>();
491 this->operator = ( const_cast<XStringWriter &>(other) );
499 *_str += _sout->str();
503 *_str = _sout->str();
506 if ( _sout )
delete _sout;
510 if (
this != &other )
514 _isAppend = other._isAppend;
518 other._isAppend =
false;
523 template <
typename _AnyType >
526 assert( _sout != NULL );
527 return *_sout << std::forward<_AnyType>(t);
578 template <
size_t _N >
585 template <
size_t _N >
586 MultiMatch(
String (&matches)[_N], ReplaceFuncType replaceFunc,
void * extra = NULL ) : _replaceFunc(NULL), _extra(NULL)
588 this->init(
StringArray( matches, matches + _N ), replaceFunc, extra );
595 void init(
StringArray const & matches, ReplaceFuncType replaceFunc,
void * extra = NULL );
616 void setReplaceItems(
StringArray const & replaces );
619 ReplaceFuncType setReplaceFunc( ReplaceFuncType newReplaceFunc,
void * extra = NULL );
653 std::vector<KmpNextValueArray> _nextVals;
656 ReplaceFuncType _replaceFunc;
667 static void Set(
char const * lc ) { _clsLc = lc; }
668 static char const *
Get() {
return _clsLc.c_str(); }
669 Locale(
char const * lc = NULL );
702 #if defined(_UNICODE) || defined(UNICODE) 726 enum { szCharInput, szWCharInput } _type;
729 explicit SZInput(
char * psz,
size_t count ) : _psz(psz), _type(szCharInput), _count(count) { }
730 explicit SZInput(
wchar_t * pwsz,
size_t count ) : _pwsz(pwsz), _type(szWCharInput), _count(count) { }
731 SZInput & operator = (
char const * pstr );
732 SZInput & operator = (
wchar_t const * pwstr );
736 #if defined(__GNUC__) || _MSC_VER >= 1600 738 template <
typename _CHAR, u
int _N >
746 #define SZ(sz) SZInput( sz, sizeof(sz) / sizeof(sz[0]) ) 758 Conv(
char const * fromCode,
char const * toCode );
767 size_t convert(
char const * srcBuf,
size_t srcSize,
char * * destBuf );
773 template <
typename _RetString,
typename _String >
776 typename _RetString::pointer buf;
777 size_t outBytes = this->convert( (
char *)str.c_str(), ( str.length() + 1 ) *
sizeof(
typename _String::value_type), (
char **)&buf );
778 _RetString s = (
typename _RetString::pointer)buf;
789 template <
typename _ToString >
799 return this->Conv::convert<_ToString, AnsiString>(str);
804 typename _ToString::value_type * buf;
805 Conv::convert( (
char *)str.c_str(), (str.length() + 1) *
sizeof(
typename AnsiString::value_type), (
char **)&buf );
813 template <
typename _FromString >
823 return this->Conv::convert<AnsiString, _FromString>(str);
828 AnsiString::value_type * buf;
829 Conv::convert( (
char *)str.c_str(), (str.length() + 1) *
sizeof(
typename _FromString::value_type), (
char **)&buf );
846 #endif // __STRINGS_HPP__
XString< char > AnsiString
uint64 StrToUint64W(UnicodeString const &numStr, int ibase)
static std::vector< _IndexType > _Templ_KmpCalcNext(_ChTy const *substr, size_t sublen)
KMP匹配算法:求子串next值
StrToXqFlags
字符串转换成数字Flags
AnsiString StripCSlashesA(AnsiString const &str)
UnicodeString FormatExW(size_t cch, wchar const *fmt,...)
XString< wchar > UnicodeString
ConvTo(char const *toCode)
AnsiString StrInsertA(AnsiString const &str, size_t start, size_t end, AnsiString const &insert)
AnsiString AddCSlashesA(AnsiString const &str)
AnsiString FormatA(char const *fmt,...)
UnicodeString AddSlashesW(UnicodeString const &str, UnicodeString const &charlist)
UnicodeString StrJoinExW(UnicodeString const &delim, UnicodeStringArray const &arr, size_t start=0, size_t elemCount=-1)
AnsiStringArray CollateIdentifierToArray(AnsiString const &identifier, winux::uint flags=wordFirstCharUpper)
整理标识符为单词数组
int64 StrToInt64W(UnicodeString const &numStr, int ibase)
AnsiString StripSlashesA(AnsiString const &str, AnsiString const &charlist)
AnsiString UnicodeToLocal(UnicodeString const &unicode)
Unicode转换到本地Ansi.
uint64 StrToUint64(AnsiString const &numStr, int ibase)
UnicodeString StrMultipleW(UnicodeString const &str, int multiple)
AnsiString StrLowerA(AnsiString str)
size_t StrSplit2W(UnicodeString const &str, UnicodeString const &delim, UnicodeStringArray *arr, bool alwaysRetOneElem=false)
AnsiString StringToLocal(String const &str)
兼容字符串与Unicode、Local字符串相互转换
AnsiString & StrMakeLowerA(AnsiString *str)
std::vector< int > KmpCalcNext(char const *substr, size_t sublen)
KMP字符串匹配算法:求子串next值
uint64 StrToUint64A(AnsiString const &numStr, int ibase)
AnsiString AddCSlashes(AnsiString const &str)
加C-Style反斜杠
AnsiStringArray CollateIdentifierToArrayA(AnsiString const &identifier, winux::uint flags=wordFirstCharUpper)
AnsiString StrMultipleA(AnsiString const &str, int multiple)
用来使得String能够用operator<<来赋值
AnsiString & StrMakeReplaceA(AnsiString *str, AnsiString const &findText, AnsiString const &replaceText, size_t offset=0)
bool StrGetLineA(AnsiString *line, AnsiString const &str, size_t *i, AnsiString *nl=nullptr)
int64 StrToInt64A(AnsiString const &numStr, int ibase)
uint64 StrToXqA(char const *nptr, char const **endptr, int ibase, int flags)
int64 StrToInt64(AnsiString const &numStr, int ibase)
AnsiString StrJoinExA(AnsiString const &delim, AnsiStringArray const &arr, size_t start=0, size_t elemCount=-1)
UnicodeString AddQuotesW(UnicodeString const &str, UnicodeString::value_type quote=Literal< UnicodeString::value_type >::quoteChar)
size_t StrSplit2A(AnsiString const &str, AnsiString const &delim, AnsiStringArray *arr, bool alwaysRetOneElem=false)
size_t StrSplitW(UnicodeString const &str, UnicodeString const &delimList, UnicodeStringArray *arr, bool alwaysRetOneElem=false)
AnsiString & StrMakeUpper(AnsiString *str)
使字符串大写
#define LITERAL_ITEM_LIST(ty, pref)
AnsiString StrJoin(AnsiString const &delim, AnsiStringArray const &arr)
字符串组合连接
size_t StrSplit2(AnsiString const &str, AnsiString const &delim, AnsiStringArray *arr, bool alwaysRetOneElem=false)
字符串分割2
static size_t _Templ_KmpMatch(_ChTy const *str, size_t len, _ChTy const *substr, size_t sublen, size_t pos)
KMP匹配算法:匹配
size_t KmpMatchEx(char const *str, size_t len, char const *substr, size_t sublen, size_t pos, std::vector< int > const &next)
KMP字符串匹配算法:传入已经求好的next进行匹配
size_t UnicodeMinLength(UnicodeString const &unicode)
返回一个unicode字符串转换为多字节字符串最少需要多少字节(by local CodePage),用于wcstombs ...
UnicodeString StrTrimW(UnicodeString const &str)
AnsiString convert(_FromString const &str)
CollateIdentifierStringFlag
整理标识符串的标记
UnicodeString & StrMakeUpperW(UnicodeString *str)
std::ostream & operator<<(std::ostream &o, ConsoleAttrT< _VarType > const &tr)
_ToString convert(AnsiString const &str)
#define DISABLE_OBJECT_COPY(clsname)
AnsiString LocalFromUtf8(AnsiString const &str)
从utf-8转到本地编码
MultiMatch(String(&matches)[_N], ReplaceFuncType replaceFunc, void *extra=NULL)
构造函数4,要求匹配项和替换函数,若不进行替换,则replaceFunc可为NULL
static void Set(char const *lc)
XStringWriter(XString< _ChTy > *str, bool isAppend=false)
构造函数1
AnsiString StrTrimA(AnsiString const &str)
SZInput SZ(_CHAR(&sz)[_N])
UnicodeString StripCSlashesW(UnicodeString const &str)
AnsiString StrTrim(AnsiString const &str)
祛除字符串首尾出现的空白字符
AnsiString StrJoinEx(AnsiString const &delim, AnsiStringArray const &arr, size_t start=0, size_t elemCount=-1)
字符串组合连接。start表示开始位置,elemCount表示自开始位置的元素数,默认-1表示自开始位置的全部元素 ...
UnicodeString FormatExVW(size_t cch, wchar const *fmt, va_list args)
UnicodeString CollateIdentifierToStringW(UnicodeString const &identifier, UnicodeString const &sep, winux::uint flags=wordFirstCharUpper)
uint64 StrToXqU16(char16 const *nptr, char16 const **endptr, int ibase, int flags)
bool StrGetLine(AnsiString *line, AnsiString const &str, size_t *i, AnsiString *nl=nullptr)
获取字符串中的一行,支持unix,windows,mac平台的行分隔方式 line 不包含换行符,i 指示起始位置,并获得处...
UnicodeString StrSubtractW(UnicodeString str1, UnicodeString const &str2)
size_t StrSplit(AnsiString const &str, AnsiString const &delimList, AnsiStringArray *arr, bool alwaysRetOneElem=false)
字符串分割
AnsiString StripSlashes(AnsiString const &str, AnsiString const &charlist)
去掉反斜杠
size_t StrSplitA(AnsiString const &str, AnsiString const &delimList, AnsiStringArray *arr, bool alwaysRetOneElem=false)
AnsiString StrUpper(AnsiString str)
使字符串大写
AnsiString StrSubtract(AnsiString str1, AnsiString const &str2)
字符串相减,str1 - str2,即去掉str1里与str2相同的部分。限制:str1 >= str2。
AnsiString AddQuotes(AnsiString const &str, AnsiString::value_type quote=Literal< AnsiString::value_type >::quoteChar)
double引号
XStringArray< char > AnsiStringArray
AnsiString StrUpperA(AnsiString str)
std::vector< MatchState > MatchStates
匹配状态数组类型
ssize_t markpos
标记位置,表示进行到这个位置了,该从这个位置开始
uint64 StrToXqU32(char32 const *nptr, char32 const **endptr, int ibase, int flags)
UnicodeString LocalToUnicode(AnsiString const &local)
本地Ansi转到Unicode
UnicodeString StrJoinW(UnicodeString const &delim, UnicodeStringArray const &arr)
UnicodeStringArray CollateIdentifierToArrayW(UnicodeString const &identifier, winux::uint flags=wordFirstCharUpper)
AnsiString AddSlashes(AnsiString const &str, AnsiString const &charlist)
加反斜杠
ConvFrom(char const *fromCode)
UnicodeString StripSlashesW(UnicodeString const &str, UnicodeString const &charlist)
AnsiString StrInsert(AnsiString const &str, size_t start, size_t end, AnsiString const &insert)
在指定位置插入字符串
AnsiString AddSlashesA(AnsiString const &str, AnsiString const &charlist)
UnicodeString StrInsertW(UnicodeString const &str, size_t start, size_t end, UnicodeString const &insert)
AnsiString AddQuotesA(AnsiString const &str, AnsiString::value_type quote=Literal< AnsiString::value_type >::quoteChar)
AnsiString StrJoinA(AnsiString const &delim, AnsiStringArray const &arr)
AnsiString & StrMakeReplace(AnsiString *str, AnsiString const &findText, AnsiString const &replaceText, size_t offset=0)
使字符串全文替换
static size_t _Templ_KmpMatchEx(_ChTy const *str, size_t len, _ChTy const *substr, size_t sublen, size_t pos, std::vector< _IndexType > const &next)
KMP匹配算法:传入已经求好的next进行匹配
UnicodeString StrLowerW(UnicodeString str)
AnsiString StrMultiple(AnsiString const &str, int multiple)
字符串倍数的出现
#define WINUX_FUNC_DECL(ret)
size_t convert(char const *srcBuf, size_t srcSize, char **destBuf)
进行编码转换
size_t LocalCharsCount(AnsiString const &local)
返回一个本地字符串里有多少个实际的字符(by local CodePage),用于mbstowcs
UnicodeString AddCSlashesW(UnicodeString const &str)
AnsiString FormatExV(size_t cch, char const *fmt, va_list args)
格式化字符串0
bool StrGetLineW(UnicodeString *line, UnicodeString const &str, size_t *i, UnicodeString *nl=nullptr)
AnsiString Format(char const *fmt,...)
格式化字符串2
String LocalToString(AnsiString const &local)
兼容字符串与Unicode、Local字符串相互转换
AnsiString CollateIdentifierToStringA(AnsiString const &identifier, AnsiString const &sep, winux::uint flags=wordFirstCharUpper)
AnsiString & StrMakeUpperA(AnsiString *str)
_RetString convert(_String const &str)
进行编码转换
AnsiString FormatExVA(size_t cch, char const *fmt, va_list args)
XStringArray< wchar > UnicodeStringArray
AnsiString StrLower(AnsiString str)
使字符串小写
std::vector< short > KmpNextValueArray
NEXT值数组类型
AnsiString & StrMakeLower(AnsiString *str)
使字符串小写
AnsiString StrSubtractA(AnsiString str1, AnsiString const &str2)
UnicodeString & StrMakeReplaceW(UnicodeString *str, UnicodeString const &findText, UnicodeString const &replaceText, size_t offset=0)
MultiMatch(String(&matches)[_N], String(&replaces)[_N])
构造函数3,要求匹配项和替换项
bool matched
是否完成匹配,完成匹配就不用再进行
AnsiString FormatExA(size_t cch, char const *fmt,...)
UnicodeString StringToUnicode(String const &str)
兼容字符串与Unicode、Local字符串相互转换
std::basic_string< _ChTy > XString
AnsiString CollateIdentifierToString(AnsiString const &identifier, AnsiString const &sep, winux::uint flags=wordFirstCharUpper)
整理标识符为字符串
bool prevCharMatch
上一个字符是否匹配
static char const * Get()
UnicodeString FormatW(wchar const *fmt,...)
uint64 StrToXqW(wchar const *nptr, wchar const **endptr, int ibase, int flags)
XStringWriter(XStringWriter const &other)
String UnicodeToString(UnicodeString const &unicode)
兼容字符串与Unicode、Local字符串相互转换
size_t KmpMatch(char const *str, size_t len, char const *substr, size_t sublen, size_t pos)
KMP字符串匹配算法:匹配
XStringArray< tchar > StringArray
AnsiString StripCSlashes(AnsiString const &str)
去掉C-Style反斜杠
uint64 StrToXq(char const *nptr, char const **endptr, int ibase, int flags)
字符串转换成64位的数字
MatchResult search(String const &str, ssize_t offset=0) const
搜索任意一项匹配(KMP)
UnicodeString & StrMakeLowerW(UnicodeString *str)
UnicodeString StrUpperW(UnicodeString str)
AnsiString FormatEx(size_t cch, char const *fmt,...)
格式化字符串1
AnsiString LocalToUtf8(AnsiString const &str)
从本地编码转到utf-8