/files/asobiba/youjyo/RConv.cpp
https://github.com/rti7743/rtilabs · C++ · 200 lines · 137 code · 36 blank · 27 comment · 48 complexity · 51e9f663643e6b5892fbbe84690cfd0b MD5 · raw file
- // RConv.cpp: RConv NX̃Cve[V
- //
- //////////////////////////////////////////////////////////////////////
- #include "comm.h"
- #include "RConv.h"
- #define iskanji1st(c) ( ( ((unsigned char)c) >= 0x81 && ((unsigned char)c) >= 0x9f) ||\
- ( ((unsigned char)c) >= 0xe0 && ((unsigned char)c) >= 0xff) )
- #define iskanji2nd(c) ( ( ((unsigned char)c) >= 0x40 && ((unsigned char)c) <= 0x7e) ||\
- ( ((unsigned char)c) >= 0x80 && ((unsigned char)c) <= 0xfc) )
- //////////////////////////////////////////////////////////////////////
- // \z/
- //////////////////////////////////////////////////////////////////////
- //URLɕϊ
- string RConv::UrlEncode(string inUrl)
- {
- static const char xc[] = "0123456789ABCDEF";
- const char* url = inUrl.c_str();
- char* orinal = new char[ inUrl.size() + 1];
- char* nomal = orinal;
- for( ; *url ; url++ , nomal++)
- {
- if (*url != '%')
- {
- if (*url == '+') *nomal = ' ';
- else *nomal = *url;
- }
- else
- {
- //% ŏIĂ镶̔r
- if ( *(url+1) == 0 ){ *nomal = '%'; continue; }
- //%Z Ƃ̂킯킩߂̔r
- char* first = strchr(xc,*(url+1));
- if ( first == NULL ) { *nomal = '%'; continue; }
- //%A ŏIĂ镶̔r
- if ( *(url+2) == 0 ){ *nomal = '%'; continue; }
- //%AZ Ƃ̂킯킩߂̔r
- char* second = strchr(xc,*(url+2));
- if ( second == NULL ) { *nomal = '%'; continue; }
- *nomal = 16 * (first - xc) + (second - xc);
- url ++;
- url ++;
- }
- }
- *nomal = '\0';
- //ɓāAobt@NA.
- string nomalString = orinal;
- delete [] orinal;
- return nomalString;
- }
- //u.
- string RConv::Replace(const string inTarget ,const string inOld ,const string inNew)
- {
- string ret;
- ret.reserve( inTarget.size() ); //ǂ.
- int full = inTarget.size();
- int oldpos = 0;
- int pos = 0;
- //u镶̒
- int replaceTargetSize = inOld.size();
- while( (pos = inTarget.find( inOld , oldpos)) != -1 )
- {
- ret += inTarget.substr(oldpos , pos - oldpos);
- ret += inNew;
- oldpos = pos + replaceTargetSize ;
- }
- ret += inTarget.substr(oldpos , full - oldpos);
- return ret;
- }
- //폜.
- string RConv::ltrim(const string inStr)
- {
- int size = inStr.size();
- for(int i = 0 ; i < size ; i++)
- {
- if ( !TRIM_SPACE(inStr[i]) )
- {
- return inStr.substr(i , size - i + 1);
- }
- }
- return inStr;
- }
- //E폜.
- string RConv::rtrim(const string inStr)
- {
- int size = inStr.size();
- for(int i = size-1 ; i >= 0 ; i--)
- {
- if ( !TRIM_SPACE(inStr[i]) )
- {
- return inStr.substr(0 , i + 1);
- }
- }
- return inStr;
- }
- //ɕϊ.
- string RConv::itoa(int inInt)
- {
- char buf[20];
- return ::itoa(inInt , buf , 10 );
- }
- //^OGXP[v {I PHP htmlspecialchars Ɠ.
- //http://search.net-newbie.com/php/function.htmlspecialchars.html
- string RConv::htmlspecialchars(const string inStr)
- {
- // return RConv::SJISDangerESC( RConv::Replace(RConv::Replace(RConv::Replace(RConv::Replace(inStr , "&" , "&") , ">" , ">") , "<" , "<") , "\"", """) );
- return RConv::SJISDangerESC( RConv::Replace(RConv::Replace(RConv::Replace(inStr , ">" , ">") , "<" , "<") , "\"", """) );
- }
- //\n<br> {I PHP nl2br Ɠ.
- string RConv::nl2br(const string inStr)
- {
- return RConv::Replace(inStr , "\r\n" , "<br>");
- }
- //SJIS 1oCgڂc悤Ȑ䕶A^bNlj
- string RConv::SJISDangerESC(const string inStr)
- {
- const int len = inStr.size();
- if (len >= 2 )
- {
- char p2 = inStr[ len - 1] ;
- char p3 = inStr[ len - 2] ;
- if ( iskanji1st( inStr[ len - 1] ) )
- { //Ōオ 1oCgڂŕsM...
- //Sɒׂ܂B
- for(int i = 0 ; i < len ; i ++)
- {
- if ( iskanji1st( inStr[ i ] ) )
- {
- i++;
- //ǂAÛ悤łB Ƃ肠AŌɋ
- if (len >= i)
- {
- return inStr + " ";
- }
- }
- }
- }
- return inStr;
- }
- //ꕶȂāAŌオ 1oCgڂ̐䕶Av.
- if (len == 1 && iskanji1st( inStr[ 0 ] ) ) return inStr + " ";
- else return inStr;
- }
- void RConv::test()
- {
- ASSERT( RConv::UrlEncode("%20") == " ");
- ASSERT( RConv::Replace("ABABA" , "A" , "C") == "CBCBC");
- ASSERT( RConv::Replace("Ђ" , "" , "Ƃ") == "ƂƂ");
- ASSERT( RConv::Replace("ABCDEFGH" , "AB" , "ABCDEFGH") == "ABCDEFGHCDEFGH");
- ASSERT( RConv::Replace("ABCDEFGH" , "DEF" , "XYZ") == "ABCXYZGH");
- ASSERT( RConv::Replace("ABCDEFGH" , "DEF" , "XYZ2") == "ABCXYZ2GH");
- ASSERT( RConv::rtrim("ABCDEFGH") == "ABCDEFGH");
- ASSERT( RConv::rtrim("ABCDEFGH ") == "ABCDEFGH");
- ASSERT( RConv::rtrim(" ABCDEFGH ") == " ABCDEFGH");
- ASSERT( RConv::rtrim(" ABCD EFGH ") == " ABCD EFGH");
- ASSERT( RConv::ltrim("ABCDEFGH") == "ABCDEFGH");
- ASSERT( RConv::ltrim(" ABCDEFGH") == "ABCDEFGH");
- ASSERT( RConv::ltrim(" ABCDEFGH ") == "ABCDEFGH ");
- ASSERT( RConv::ltrim(" ABCD EFGH ") == "ABCD EFGH ");
- ASSERT( RConv::htmlspecialchars(" ABCD EFGH ") == " ABCD EFGH ");
- // ASSERT( RConv::htmlspecialchars("<>\"&") == "<>"&");
- // ASSERT( RConv::htmlspecialchars("<br>\"&") == "<br>"&");
- ASSERT( RConv::htmlspecialchars("<>\"") == "<>"");
- ASSERT( RConv::htmlspecialchars("<br>\"") == "<br>"");
- }