My Project
Classes | Static Public Member Functions | List of all members
ParaEngine::StringHelper Class Reference

string manipulation helpers for ParaEngine More...

#include <StringHelper.h>

Static Public Member Functions

static bool UTF8ToUTF16 (const std::string &utf8, std::u16string &outUtf16)
 Converts utf8 string to utf16 string. More...
 
static bool UTF8ToUTF16_Safe (const std::string &utf8, std::u16string &outUtf16)
 similar to UTF8ToUTF16. More...
 
static void TrimUTF16VectorFromIndex (std::vector< char16_t > &str, int index)
 
static bool IsUnicodeSpace (char16_t ch)
 
static bool IsCJKUnicode (char16_t ch)
 
static void TrimUTF16Vector (std::vector< char16_t > &str)
 
static unsigned int GetIndexOfLastNotChar16 (const std::vector< char16_t > &str, char16_t c)
 
static bool UTF16ToUTF8 (const std::u16string &utf16, std::string &outUtf8)
 Converts utf16 string to utf8 string. More...
 
static const WCHAR * MultiByteToWideChar (const char *name, unsigned int nCodePage=0, size_t *outLen=nullptr)
 
static const char * WideCharToMultiByte (const WCHAR *name, unsigned int nCodePage=0, size_t *outLen=nullptr)
 
static int WideCharToMultiByte (const WCHAR *name, char *szText, int nLength, unsigned int nCodePage=0)
 get the text of the control More...
 
static const WCHAR * AnsiToWideChar (const char *name)
 
static const char * WideCharToAnsi (const WCHAR *name)
 
static const char * UTF8ToAnsi (const char *name)
 
static const char * AnsiToUTF8 (const char *name)
 
static int GetUnicodeCharNum (const char *str)
 get the number of characters in str. More...
 
static string UniSubString (const char *str, int nFrom, int nTo)
 same as LUA string.sub(), except that the index is character. More...
 
static string SimpleEncode (const string &source)
 encode a string using really simple algorithm. More...
 
static string SimpleDecode (const string &source)
 decode a string using really simple algorithm. More...
 
static const std::string & EncodingConvert (const std::string &srcEncoding, const std::string &dstEncoding, const std::string &bytes)
 Converts an entire byte array from one encoding to another. More...
 
static bool CopyTextToClipboard (const string &text)
 copy text to clipboard. More...
 
static const char * GetTextFromClipboard ()
 get text from clipboard. More...
 
static void DevideString (const string &input, string &str1, string &str2, char separator=';')
 divide string
 
static void split (const std::string &src, const std::string &token, std::vector< std::string > &vect)
 splite string by token
 
static void make_lower (string &str)
 Makes the string lower case.
 
static void make_upper (string &str)
 Makes the string upper case.
 
static bool checkValidXMLChars (const std::string &data)
 Checks whether the given input is valid UTF-8. More...
 
static bool removeInValidXMLChars (std::string &data)
 remove invalid characters in the given input. More...
 
static bool IsNumber (const char *str)
 
static bool IsLetter (const char *str)
 
static int StrToInt (const char *str)
 
static double StrToFloat (const char *str)
 
static bool RegularMatch (const char *input, const char *expression)
 
static std::string ToCString (const char *input)
 
static std::string StrReplace (const char *inputstring, char srcchar, char destchar)
 
static RECTGetImageAndRect (const std::string &str, std::string &imagefile, RECT *pOut=NULL)
 
static std::string md5 (const std::string &source, bool bBinary=false)
 convert the md5 of the input source string. More...
 
static std::string sha1 (const std::string &source, bool bBinary=false)
 
static std::string base64 (const std::string &source)
 convert the base64 of the input source string. More...
 
static std::string unbase64 (const std::string &source)
 return unbase64 of the input source string. More...
 
static void fast_sprintf (char *s, const char *fmt,...)
 a fast printf function that support limited functions. More...
 
static void fast_snprintf (char *s, int nMaxCount, const char *fmt,...)
 e.g. More...
 
static int fast_itoa (int64 value, char *result, int buf_size, int base=10)
 fast itoa modified from http://code.google.com/p/maxmods/ More...
 
static int fast_dtoa (double num, char *str, int nBuffSize=40, int max_decimal=5, int radix=10)
 doing a fast (may not be accurate) double to string conversion. More...
 
static bool MatchWildcard (const std::string &str, const std::string &sWildcardPattern)
 check if the input string matches the wild card pattern More...
 
static bool StrEndsWith (const string &str, const string &sequence)
 check whether str ends with sequence More...
 

Detailed Description

string manipulation helpers for ParaEngine

Member Function Documentation

§ base64()

std::string ParaEngine::StringHelper::base64 ( const std::string &  source)
static

convert the base64 of the input source string.

§ checkValidXMLChars()

bool ParaEngine::StringHelper::checkValidXMLChars ( const std::string &  data)
static

Checks whether the given input is valid UTF-8.

Parameters
dataThe data to check for validity.
Returns
@b True if the input is valid UTF-8, false otherwise.

§ CopyTextToClipboard()

bool ParaEngine::StringHelper::CopyTextToClipboard ( const string &  text)
static

copy text to clipboard.

Input is GUI UTF8 encoding

§ EncodingConvert()

const std::string & StringHelper::EncodingConvert ( const std::string &  srcEncoding,
const std::string &  dstEncoding,
const std::string &  bytes 
)
static

Converts an entire byte array from one encoding to another.

Parameters
srcEncodingany encoding name. If nil or "", it is the default coding in NPL. see Encoding.GetEncoding(). Below are some commonly used field | Code Page | Name | | 950 | big5 | | 936 | gb2312 | | 65001 | utf-8 | | 65005 | utf-32 | There is one special code name called "HTML", which contains HTML special characters in ascii code page. This is usually true for most "iso8859-15" encoding in western worlds. It just writes the unicode number+XXXX in ascii character "&#XXXX;" where & is optional.
dstEncodingsave as above. If nil or "", it will be converted to default coding in NPL. : the source bytes. e.g. The most common use of this function is to create HTML special character to NPL string, like below local text = ParaMisc.EncodingConvert("HTML", "", "Chinese characters: &#24320;&#21457;") log(text);

§ fast_dtoa()

int ParaEngine::StringHelper::fast_dtoa ( double  num,
char *  result,
int  buf_size = 40,
int  max_decimal = 5,
int  base = 10 
)
static

doing a fast (may not be accurate) double to string conversion.

by Xizhi, 2010.11.17 use fast_itoa to compute fast_dtoa.

it is similar to "%.xxf", except that it will remove all trailing zero, and ensures that nBuffSize is not exceed for out of range numbers, etc.

Parameters
numValue to be converted to a string. NaN, +Inf,-Inf will generate "0"
stroutput string, array in memory where to store the resulting null-terminated string.
nBuffSizethe output buff size.
max_decimalmax number of numbers after the decimal point. It will remove trailing '0'
radixthe radix.
Returns
the number of characters(not including the trailing '\0') in result.

Please note accuracy is not guaranteed. I.e.0 0.999 may be rounded to 1 current only base 10 is supported.

§ fast_itoa()

int ParaEngine::StringHelper::fast_itoa ( int64  value,
char *  result,
int  buf_size,
int  base = 10 
)
static

fast itoa modified from http://code.google.com/p/maxmods/

Parameters
valuethe value to be converted.
resultthe array to hold the result.
buf_sizethe result buffer size.
basecan only be [2,16]
Returns
the number of characters(not including the trailing '\0') in result.

§ fast_snprintf()

void ParaEngine::StringHelper::fast_snprintf ( char *  s,
int  nMaxCount,
const char *  fmt,
  ... 
)
static

e.g.

fast_snprintf(line, MAX_LINE, "%f ok", 10.f);

§ fast_sprintf()

void ParaEngine::StringHelper::fast_sprintf ( char *  s,
const char *  fmt,
  ... 
)
static

a fast printf function that support limited functions.

The formats supported by this implementation are: 'd' 'u' 'c' 's' 'x' 'X' 'f'. Zero padding and field width are also supported. One very important feature is that f behaves like d if float is integer

§ GetTextFromClipboard()

const char * ParaEngine::StringHelper::GetTextFromClipboard ( )
static

get text from clipboard.

text is converted to ANSI code page when returned.

§ GetUnicodeCharNum()

int StringHelper::GetUnicodeCharNum ( const char *  str)
static

get the number of characters in str.

Str is assumed to be in ANSI code page. it is converted to Unicode and return the character count.

§ MatchWildcard()

bool ParaEngine::StringHelper::MatchWildcard ( const std::string &  str,
const std::string &  sWildcardPattern 
)
static

check if the input string matches the wild card pattern

Parameters
sWildcardPatternsuch as "*.*" or "*" or "main_*.pkg", etc.

§ md5()

string ParaEngine::StringHelper::md5 ( const std::string &  source,
bool  bBinary = false 
)
static

convert the md5 of the input source string.

Parameters
bBinaryif false (default), result is 32 hex number chars. if true, result is 16 bytes binary string.

§ removeInValidXMLChars()

bool ParaEngine::StringHelper::removeInValidXMLChars ( std::string &  data)
static

remove invalid characters in the given input.

Parameters
dataThe data to check for validity.
Returns
@b True if the input is valid UTF-8, false otherwise. If false, invalid chars in input will be removed.

§ SimpleDecode()

std::string StringHelper::SimpleDecode ( const string &  source)
static

decode a string using really simple algorithm.

str = SimpleDecode(SimpleEncode(str))

Returns
: it may return NULL if input invalid

§ SimpleEncode()

std::string StringHelper::SimpleEncode ( const string &  source)
static

encode a string using really simple algorithm.

it just makes the source ineligible. It is still not immune to crackers. str = SimpleDecode(SimpleEncode(str))

Returns
: it may return NULL if input invalid

§ StrEndsWith()

bool ParaEngine::StringHelper::StrEndsWith ( const string &  str,
const string &  sequence 
)
static

check whether str ends with sequence

Parameters
sequenceit may contain ?, which matches to 1 any character.

§ unbase64()

std::string ParaEngine::StringHelper::unbase64 ( const std::string &  source)
static

return unbase64 of the input source string.

§ UniSubString()

std::string StringHelper::UniSubString ( const char *  str,
int  nFrom,
int  nTo 
)
static

same as LUA string.sub(), except that the index is character.

get a sub string of a ANSI Code page string. However, the index are unicode characters.

Parameters
strthe string to use
nFromcharacter index beginning from 1.

§ UTF16ToUTF8()

bool ParaEngine::StringHelper::UTF16ToUTF8 ( const std::u16string &  utf16,
std::string &  outUtf8 
)
static

Converts utf16 string to utf8 string.

Parameters
utf16The utf16 string to be converted
outUtf8The output utf8 string
Returns
true if succeed, otherwise false
Note
Please check the return value before using outUtf8 e.g.
std::string utf8;
bool ret = StringHelper::UTF16ToUTF8(u"\u4f60\u597d", utf8);
if (ret) {
do_some_thing_with_utf8(utf8);
}

§ UTF8ToUTF16()

bool ParaEngine::StringHelper::UTF8ToUTF16 ( const std::string &  utf8,
std::u16string &  outUtf16 
)
static

Converts utf8 string to utf16 string.

Parameters
utf8The utf8 string to be converted
outUtf16The output utf16 string
Returns
true if succeed, otherwise false
Note
Please check the return value before using outUtf16 e.g.
std::u16string utf16;
bool ret = StringHelper::UTF8ToUTF16("ÄãºÃhello", utf16);
if (ret) {
do_some_thing_with_utf16(utf16);
}

§ UTF8ToUTF16_Safe()

bool ParaEngine::StringHelper::UTF8ToUTF16_Safe ( const std::string &  utf8,
std::u16string &  outUtf16 
)
static

similar to UTF8ToUTF16.

except that if UTF8ToUTF16 returns false, it will replace invalid utf8 character with ?. and then return the converted string.

§ WideCharToMultiByte()

int ParaEngine::StringHelper::WideCharToMultiByte ( const WCHAR *  name,
char *  szText,
int  nLength,
unsigned int  nCodePage = 0 
)
static

get the text of the control

Parameters
szText[out] buffer to receive the text
nLengthsize of the input buffer
Returns
return the number of bytes written to the buffer. If nLength <= 0, this function returns the total number of characters of the text

The documentation for this class was generated from the following files: