1 #ifndef __STRINGS_HPP__ 2 #define __STRINGS_HPP__ 15 template <
typename _ChTy >
23 template <
typename _RetChTy,
typename _ChTy = _RetChTy >
26 _RetChTy str[
sizeof(ch) * 2 + 1] = { (_RetChTy)0 };
30 bool hadNonZero = padZero;
33 if ( pbyt[i] && !hadNonZero ) hadNonZero =
true;
50 template <
typename _ChTy >
68 #if defined(_UNICODE) || defined(UNICODE) 75 template <
typename _ChTy >
92 #if defined(_UNICODE) || defined(UNICODE) 99 template <
typename _ChTy >
117 #if defined(_UNICODE) || defined(UNICODE) 124 template <
typename _ChTy >
141 #if defined(_UNICODE) || defined(UNICODE) 148 template <
typename _ChTy >
159 #if defined(_UNICODE) || defined(UNICODE) 166 template <
typename _ChTy >
177 #if defined(_UNICODE) || defined(UNICODE) 184 template <
typename _ChTy >
195 #if defined(_UNICODE) || defined(UNICODE) 202 template <
typename _ChTy >
213 #if defined(_UNICODE) || defined(UNICODE) 220 template <
typename _ChTy >
231 template <
typename _ChTy >
242 template <
typename _ChTy >
257 #if defined(_UNICODE) || defined(UNICODE) 258 inline UnicodeString StrTrim(
UnicodeString const & str,
size_t * leftSpaceCount =
nullptr,
size_t * rightSpaceCount =
nullptr ) {
return StrTrim<wchar>( str, leftSpaceCount, rightSpaceCount ); }
262 inline AnsiString StrTrim(
AnsiString const & str,
size_t * leftSpaceCount =
nullptr,
size_t * rightSpaceCount =
nullptr ) {
return StrTrim<char>( str, leftSpaceCount, rightSpaceCount ); }
268 template <
typename _ChTy >
279 #if defined(_UNICODE) || defined(UNICODE) 286 template <
typename _ChTy >
297 #if defined(_UNICODE) || defined(UNICODE) 304 template <
typename _ChTy >
315 #if defined(_UNICODE) || defined(UNICODE) 322 template <
typename _ChTy >
333 #if defined(_UNICODE) || defined(UNICODE) 340 template <
typename _ChTy >
351 #if defined(_UNICODE) || defined(UNICODE) 358 template <
typename _ChTy >
369 #if defined(_UNICODE) || defined(UNICODE) 385 template <
typename _ChTy >
386 uint64 StrToXq( _ChTy
const * nptr, _ChTy
const ** endptr,
int ibase,
int flags );
396 #if defined(_UNICODE) || defined(UNICODE) 397 inline uint64 StrToXq(
wchar const * nptr,
wchar const ** endptr,
int ibase,
int flags ) {
return StrToXq<wchar>( nptr, endptr, ibase, flags ); }
399 inline uint64 StrToXq(
char const * nptr,
char const ** endptr,
int ibase,
int flags ) {
return StrToXq<char>( nptr, endptr, ibase, flags ); }
403 template <
typename _ChTy >
413 #if defined(_UNICODE) || defined(UNICODE) 420 template <
typename _ChTy >
430 #if defined(_UNICODE) || defined(UNICODE) 437 template <
typename _ChTy >
448 #if defined(_UNICODE) || defined(UNICODE) 455 template <
typename _ChTy >
458 #if defined(_UNICODE) || defined(UNICODE) 465 template <
typename _ChTy >
476 #if defined(_UNICODE) || defined(UNICODE) 483 template <
typename _ChTy >
486 #if defined(_UNICODE) || defined(UNICODE) 493 template <
typename _ChTy >
504 #if defined(_UNICODE) || defined(UNICODE) 511 template <
typename _ChTy >
533 #if defined(_UNICODE) || defined(UNICODE) 540 template <
typename _ChTy >
563 #if defined(_UNICODE) || defined(UNICODE) 564 inline bool StrGetLine(
UnicodeString * line, UnicodeString::value_type
const * str,
size_t len,
size_t * i,
UnicodeString * nl =
nullptr ) {
return StrGetLine<wchar>( line, str, len, i, nl ); }
566 inline bool StrGetLine(
AnsiString * line, AnsiString::value_type
const * str,
size_t len,
size_t * i,
AnsiString * nl =
nullptr ) {
return StrGetLine<char>( line, str, len, i, nl ); }
577 template <
typename _ChTy >
605 template <
typename _ChTy >
616 #if defined(_UNICODE) || defined(UNICODE) 623 template <
typename _ChTy >
634 #if defined(_UNICODE) || defined(UNICODE) 642 template <
typename _IndexType,
typename _ChTy >
645 std::vector<_IndexType> next( sublen + 1 );
650 if ( k == -1 || substr[j] == substr[k] )
655 next[j] = (_IndexType)k;
668 template <
typename _IndexType,
typename _ChTy >
669 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 )
676 if ( str[i] == substr[j] )
696 template <
typename _IndexType,
typename _ChTy >
697 inline static size_t _Templ_KmpMatch( _ChTy
const * str,
size_t len, _ChTy
const * substr,
size_t sublen,
size_t pos )
699 return _Templ_KmpMatchEx<_IndexType, _ChTy>( str, len, substr, sublen, pos,
_Templ_KmpCalcNext( substr, sublen ) );
703 template <
typename _ChTy >
704 inline std::vector<int>
KmpCalcNext( _ChTy
const * substr,
size_t sublen )
706 return _Templ_KmpCalcNext< int, _ChTy >( substr, sublen );
709 template <
typename _ChTy >
710 inline size_t KmpMatchEx( _ChTy
const * str,
size_t len, _ChTy
const * substr,
size_t sublen,
size_t pos, std::vector<int>
const & next )
712 return _Templ_KmpMatchEx< int, _ChTy >( str, len, substr, sublen, pos, next );
715 template <
typename _ChTy >
716 inline size_t KmpMatch( _ChTy
const * str,
size_t len, _ChTy
const * substr,
size_t sublen,
size_t pos )
718 return KmpMatchEx<_ChTy>( str, len, substr, sublen, pos,
KmpCalcNext( substr, sublen ) );
723 template <
typename _ChTy >
733 _sout =
new std::basic_ostringstream<_ChTy>();
737 this->
operator = ( const_cast<XStringWriter &>(other) );
745 *_str += _sout->str();
749 *_str = _sout->str();
752 if ( _sout )
delete _sout;
756 if (
this != &other )
760 _isAppend = other._isAppend;
764 other._isAppend =
false;
769 template <
typename _AnyType >
772 return *_sout << std::forward<_AnyType>(t);
777 std::basic_ostringstream<_ChTy> * _sout;
809 memset(
this, 0,
sizeof(*
this) );
839 template <
size_t _Count >
846 template <
size_t _Count >
847 MultiMatch(
String (&matches)[_Count], ReplaceFuncType replaceFunc,
void * extra = NULL ) : _replaceFunc(NULL), _extra(NULL)
849 this->init(
StringArray( matches, matches + _Count ), replaceFunc, extra );
856 void init(
StringArray const & matches, ReplaceFuncType replaceFunc,
void * extra = NULL );
877 void setReplaceItems(
StringArray const & replaces );
880 ReplaceFuncType setReplaceFunc( ReplaceFuncType newReplaceFunc,
void * extra = NULL );
914 std::vector<KmpNextValueArray> _nextVals;
915 mutable MatchStates _states;
918 ReplaceFuncType _replaceFunc;
926 static void Set(
char const * lc ) { _clsLc = lc; }
927 static char const *
Get() {
return _clsLc.c_str(); }
928 Locale(
char const * lc = NULL );
967 #if defined(_UNICODE) || defined(UNICODE) 987 explicit SZInput(
char * psz,
size_t count ) : _psz(psz), _type(szCharInput), _count(count) { }
988 explicit SZInput(
wchar_t * pwsz,
size_t count ) : _pwsz(pwsz), _type(szWCharInput), _count(count) { }
998 enum { szCharInput, szWCharInput } _type;
1002 #if defined(__GNUC__) || _MSC_VER >= 1600 1004 template <
typename _CHAR, u
int _Count >
1012 #define SZ(sz) SZInput( sz, sizeof(sz) / sizeof(sz[0]) ) 1033 size_t convert(
char const * srcBuf,
size_t srcSize,
char * * destBuf );
1039 template <
typename _RetString,
typename _String >
1042 typename _RetString::pointer buf;
1043 size_t n = this->convert( (
char *)str.c_str(), str.length() *
sizeof(
typename _String::value_type), (
char **)&buf );
1044 _RetString s( (
typename _RetString::pointer)buf, n /
sizeof(
typename _RetString::value_type) );
1056 template <
typename _ToString >
1066 return this->Conv::convert<_ToString, AnsiString>(str);
1071 typename _ToString::value_type * buf;
1072 size_t n =
Conv::convert( (
char *)str.c_str(), str.length() *
sizeof(
typename AnsiString::value_type), (
char **)&buf );
1073 _ToString s( buf, n /
sizeof(
typename _ToString::value_type) );
1080 template <
typename _FromString >
1090 return this->Conv::convert<AnsiString, _FromString>(str);
1095 AnsiString::value_type * buf;
1096 size_t n =
Conv::convert( (
char *)str.c_str(), str.length() *
sizeof(
typename _FromString::value_type), (
char **)&buf );
1097 AnsiString s( buf, n /
sizeof(AnsiString::value_type) );
1112 Utf8String toUtf8(
size_t newCap = 0 )
const;
1117 size_t calcUtf8Length()
const;
1118 size_t calcUnicodeLength()
const;
1119 size_t calcUtf16Length()
const;
1120 size_t calcUtf32Length()
const;
1123 template <
typename _Ty >
1124 _Ty
const * _q()
const {
return reinterpret_cast<_Ty
const *
>(_p); }
1138 #define $u8(s) winux::UnicodeConverter(s).toUtf8() 1139 #define $L(s) winux::UnicodeConverter(s).toUnicode() 1140 #define $u(s) winux::UnicodeConverter(s).toUtf16() 1141 #define $U(s) winux::UnicodeConverter(s).toUtf32() 1146 #if defined(OS_WIN) && !defined(LOCAL_IS_UTF8) || defined(LOCAL_ISNT_UTF8) 1151 #define LOCAL_FROM_UTF8(s) winux::LocalFromUtf8(s) 1152 #define LOCAL_TO_UTF8(s) winux::LocalToUtf8(s) 1158 #define LOCAL_FROM_UTF8(s) s 1159 #define LOCAL_TO_UTF8(s) s 1165 #endif // __STRINGS_HPP__
XString< char > AnsiString
XStringWriter & operator=(XStringWriter &other)
MultiMatch(String(&matches)[_Count], String(&replaces)[_Count])
构造函数3,要求匹配项和替换项
void GetLineByPos(XString< _ChTy > const &content, ssize_t pos, ssize_t *lineHead, ssize_t *lineTail, XString< _ChTy > *line=nullptr)
获取指定位置的行内容
XString< _ChTy > StrTrim(XString< _ChTy > const &str, size_t *leftSpaceCount=nullptr, size_t *rightSpaceCount=nullptr)
static std::vector< _IndexType > _Templ_KmpCalcNext(_ChTy const *substr, size_t sublen)
KMP匹配算法:求子串next值
StrToXqFlags
字符串转换成数字Flags
XString< _ChTy > StrRTrim(XString< _ChTy > const &str, size_t *rightSpaceCount=nullptr)
UnicodeString FormatExW(size_t cch, wchar const *fmt,...)
XString< wchar > UnicodeString
AnsiString FormatA(char const *fmt,...)
XString< _ChTy > StrJoinEx(XString< _ChTy > const &delim, XStringArray< _ChTy > const &arr, size_t start=0, size_t elemCount=-1)
XString< _ChTy > CollateIdentifierToString(XString< _ChTy > const &identifier, XString< _ChTy > const &sep, winux::uint flags=wordFirstCharUpper)
XString< _ChTy > StrInsert(XString< _ChTy > const &str, size_t start, size_t end, XString< _ChTy > const &insert)
AnsiString UnicodeToLocal(UnicodeString const &unicode)
Unicode转换到本地Ansi.
AnsiString StringToLocal(String const &str)
兼容字符串与Unicode、Local字符串相互转换
XString< _ChTy > AddSlashes(XString< _ChTy > const &str, XString< _ChTy > const &charlist)
size_t KmpMatchEx(_ChTy const *str, size_t len, _ChTy const *substr, size_t sublen, size_t pos, std::vector< int > const &next)
KMP字符串匹配算法:传入已经求好的next进行匹配
用来使得String能够用operator<<来赋值
XString< _ChTy > StrJoin(XString< _ChTy > const &delim, XStringArray< _ChTy > const &arr)
SZInput SZ(_CHAR(&sz)[_Count])
VC2010以上支持模板取数组大小
XString< _ChTy > StrLower(XString< _ChTy > str)
static size_t _Templ_KmpMatch(_ChTy const *str, size_t len, _ChTy const *substr, size_t sublen, size_t pos)
KMP匹配算法:匹配
ConvFrom(AnsiString const &fromCode)
XString< _ChTy > StripCSlashes(XString< _ChTy > const &str)
bool StrGetLine(XString< _ChTy > *line, XString< _ChTy > const &str, size_t *i, XString< _ChTy > *nl=nullptr)
std::vector< int > KmpCalcNext(_ChTy const *substr, size_t sublen)
KMP字符串匹配算法:求子串next值
size_t UnicodeMinLength(UnicodeString const &unicode)
返回一个unicode字符串转换为多字节字符串最少需要多少字节(by local CodePage),用于wcstombs ...
static void Free(void *p)
AnsiString convert(_FromString const &str)
CollateIdentifierStringFlag
整理标识符串的标记
std::vector< XString< _ChTy > > XStringArray
int64 StrToInt64(XString< _ChTy > const &numStr, int ibase)
_ToString convert(AnsiString const &str)
#define DISABLE_OBJECT_COPY(clsname)
uint64 StrToXq(_ChTy const *nptr, _ChTy const **endptr, int ibase, int flags)
static void Set(char const *lc)
XStringWriter(XString< _ChTy > *str, bool isAppend=false)
构造函数1
size_t KmpMatch(_ChTy const *str, size_t len, _ChTy const *substr, size_t sublen, size_t pos)
KMP字符串匹配算法:匹配
UnicodeString FormatExVW(size_t cch, wchar const *fmt, va_list args)
Unicode转换器。可以使UTF-8、UTF-16、UTF-32字符串相互自由转换,字节序为平台默认
XString< _ChTy > StrMultiple(XString< _ChTy > const &str, size_t multiple)
XString< char32 > Utf32String
XString< _ChTy > StrUpper(XString< _ChTy > str)
static constexpr size_t const npos
非位置,值为-1。
std::basic_string< _ChTy, std::char_traits< _ChTy >, std::allocator< _ChTy > > XString
XStringArray< char > AnsiStringArray
std::vector< MatchState > MatchStates
匹配状态数组类型
XString< _ChTy > & StrMakeLower(XString< _ChTy > *str)
size_t StrSplit2(XString< _ChTy > const &str, XString< _ChTy > const &delim, XStringArray< _ChTy > *arr, bool alwaysRetOneElem=false)
ssize_t markpos
标记位置,表示进行到这个位置了,该从这个位置开始
XString< _ChTy > & StrMakeReplace(XString< _ChTy > *str, XString< _ChTy > const &findText, XString< _ChTy > const &replaceText, size_t offset=0)
XString< _ChTy > StrSubtract(XString< _ChTy > str1, XString< _ChTy > const &str2)
UnicodeString LocalToUnicode(AnsiString const &local)
本地Ansi转到Unicode
XString< _ChTy > & StrMakeUpper(XString< _ChTy > *str)
XString< char > Utf8String
static XString< _ChTy > CharSeqToString(_ChTy const *str, size_t len=npos)
C字符串转到XString.
static AnsiString LocalToUtf8(AnsiString const &str)
从本地编码转到utf-8
ConvTo(AnsiString const &toCode)
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进行匹配
XString< _ChTy > AddQuotes(XString< _ChTy > const &str, _ChTy quote=Literal< _ChTy >::quoteChar)
std::basic_ostream< _ChTy > & operator<<(_AnyType &&t)
#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
MultiMatch(String(&matches)[_Count], ReplaceFuncType replaceFunc, void *extra=NULL)
构造函数4,要求匹配项和替换函数,若不进行替换,则replaceFunc可为NULL
XString< _ChTy > AddCSlashes(XString< _ChTy > const &str)
AnsiString FormatExV(size_t cch, char const *fmt, va_list args)
格式化字符串0
XStringArray< _ChTy > CollateIdentifierToArray(XString< _ChTy > const &identifier, winux::uint flags=wordFirstCharUpper)
XString< _ChTy > StrLTrim(XString< _ChTy > const &str, size_t *leftSpaceCount=nullptr)
AnsiString Format(char const *fmt,...)
格式化字符串2
String LocalToString(AnsiString const &local)
兼容字符串与Unicode、Local字符串相互转换
size_t StrSplit(XString< _ChTy > const &str, XString< _ChTy > const &delimList, XStringArray< _ChTy > *arr, bool alwaysRetOneElem=false)
_RetString convert(_String const &str)
进行编码转换
AnsiString FormatExVA(size_t cch, char const *fmt, va_list args)
XStringArray< wchar > UnicodeStringArray
std::vector< short > KmpNextValueArray
NEXT值数组类型
XString< _ChTy > StripSlashes(XString< _ChTy > const &str, XString< _ChTy > const &charlist)
MatchResult commonSearch(String const &str, ssize_t offset=0) const
搜索任意一项匹配(贪婪模式,非KMP算法)
bool matched
是否完成匹配,完成匹配就不用再进行
static AnsiString LocalFromUtf8(AnsiString const &str)
从utf-8转到本地编码
static XString< _RetChTy > CharToHexStr(_ChTy ch, bool padZero=false)
字符转成16进制串形式
AnsiString FormatExA(size_t cch, char const *fmt,...)
UnicodeString StringToUnicode(String const &str)
兼容字符串与Unicode、Local字符串相互转换
bool prevCharMatch
上一个字符是否匹配
static char const * Get()
UnicodeString FormatW(wchar const *fmt,...)
XStringWriter(XStringWriter const &other)
String UnicodeToString(UnicodeString const &unicode)
兼容字符串与Unicode、Local字符串相互转换
XStringArray< tchar > StringArray
uint64 StrToUInt64(XString< _ChTy > const &numStr, int ibase)
XString< char16 > Utf16String
AnsiString FormatEx(size_t cch, char const *fmt,...)
格式化字符串1