Skip to content

Commit 1fa819f

Browse files
committed
stringUtil add quote and unquote function
1 parent 580ec8e commit 1fa819f

7 files changed

Lines changed: 89 additions & 7 deletions

File tree

‎cpp_util/string_util/stringUtil.cpp‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
USING_NAMESPACE(std)
99
NAMESPACE_SETUP(Util)
1010

11+
const string StringUtil::QUOTE_SAVE_CHARS = "-_.;/?:@&=+$,";
12+
1113
bool StringUtil::StringFormat(string& res, const char* fmt, ...) {
1214
// default size 256 bytes
1315
size_t size = 256;
@@ -18,7 +20,7 @@ bool StringUtil::StringFormat(string& res, const char* fmt, ...) {
1820
int n = vsnprintf((char *)res.c_str(), size, fmt, ap);
1921
va_end(ap);
2022
// success write to res and buffer < size
21-
if (n > -1 && n < size) {
23+
if (n > -1 && n < (int)size) {
2224
res.resize(n);
2325
return true;
2426
}
@@ -45,7 +47,7 @@ string StringUtil::StringFormat(const char* fmt, ...) {
4547
int n = vsnprintf((char *)res.c_str(), size, fmt, ap);
4648
va_end(ap);
4749
// success write to res and buffer < size
48-
if (n > -1 && n < size) {
50+
if (n > -1 && n < (int)size) {
4951
res.resize(n);
5052
return res;
5153
}
@@ -351,28 +353,48 @@ bool StringUtil::UnicodeToGBK(UnicodeContainerIter begin, UnicodeContainerIter e
351353
return true;
352354
}
353355

356+
char StringUtil::ToHex(unsigned int value) {
357+
return value > 9 ? value-10+'A' : value+'0';
358+
}
359+
360+
unsigned int StringUtil::FromHex(char hex) {
361+
return isdigit(hex) ? hex-'0' : hex-'A'+10;
362+
}
363+
354364
string StringUtil::Quote(const string& rawStr) {
355365
string quoteStr = "";
356-
#if 0
357366
size_t rawStrSz = rawStr.size();
358367
for (size_t i = 0; i < rawStrSz; i++) {
359368
char c = rawStr[i];
360369
char buff[4];
361370
memset(buff, '\0', 4);
362-
if (isalnum(c) || unquoteChars.find(string(c)) != unquoteChars.end()) {
371+
if (isalnum(c) || IsInStr(QUOTE_SAVE_CHARS, c)) {
363372
buff[0] = c;
364373
}
365374
else {
366375
buff[0] = '%';
376+
buff[1] = ToHex((unsigned int)c >> 4);
377+
buff[2] = ToHex((unsigned int)c % 16);
367378
}
368379
quoteStr += string(buff);
369380
}
370-
#endif
371381
return quoteStr;
372382
}
373383

374384
string StringUtil::UnQuote(const string& quoteStr) {
375385
string rawStr = "";
386+
size_t quoteStrSz = quoteStr.size();
387+
for (size_t i = 0; i < quoteStrSz; i++) {
388+
char c = quoteStr[i];
389+
if (c != '%') {
390+
rawStr += c;
391+
}
392+
else {
393+
i++;
394+
rawStr += (char)((FromHex(quoteStr[i]) << 4) + (FromHex(quoteStr[i+1])));
395+
i++;
396+
}
397+
}
376398
return rawStr;
377399
}
378400

‎cpp_util/string_util/stringUtil.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <ctype.h>
1010
#include <stdint.h>
1111
#include <stdarg.h>
12+
#include <string.h>
1213
#include <string>
1314
#include <vector>
1415
#include <iostream>
@@ -176,6 +177,20 @@ class StringUtil {
176177
* */
177178
static bool UnicodeToGBK(UnicodeContainerIter begin, UnicodeContainerIter end, string& res);
178179

180+
/*
181+
* 1. unsigned int value to Hex char
182+
* 0~15 -> '0'~'F'
183+
* 2. return char
184+
* */
185+
static char ToHex(unsigned int value);
186+
187+
/*
188+
* 1. from hex to unsigned int
189+
* '0'~'F' -> 0~15
190+
* 2. return unsigned int
191+
* */
192+
static unsigned int FromHex(char hex);
193+
179194
/*
180195
* 1. quote string
181196
* 2. return quote string
@@ -188,6 +203,9 @@ class StringUtil {
188203
* */
189204
static string UnQuote(const string& quoteStr);
190205

206+
private:
207+
static const string QUOTE_SAVE_CHARS;
208+
191209
}; //class StringUtil
192210

193211
template<typename T>
0 Bytes
Binary file not shown.

‎cpp_util/string_util/test/md5Test.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ TEST(TestMd5, Md5File){
4141
filePath = "testdata/test_1";
4242
hashVal = "";
4343
ASSERT_TRUE(Md5File(filePath.c_str(), hashVal));
44-
ASSERT_EQ(hashVal, "fd35e9b80d13dece09300e31f10f256b");
44+
ASSERT_EQ(hashVal, "2490a3d39b0004e4afeb517ef0ddbe2d");
4545

4646
// hash nonempty filePath
4747
filePath = "testdata/test_2";
4848
hashVal = "";
4949
ASSERT_TRUE(Md5File(filePath.c_str(), hashVal));
50-
ASSERT_EQ(hashVal, "96fb54c6cafb4b1425d85fd6e2d4f228");
50+
ASSERT_EQ(hashVal, "b0b3b0dbf5330e3179c6ae3e0ac524c9");
5151
}

‎cpp_util/string_util/test/stringUtilTest.cpp‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,43 @@ TEST(TestStringUtil, UnicodeToUtf8) {
553553
ASSERT_TRUE(StringUtil::UnicodeToUtf8(begin, end, res));
554554
ASSERT_EQ(res, "陈国林");
555555
}
556+
557+
TEST(TestStringUtil, ToHex) {
558+
unsigned int value = 6;
559+
ASSERT_EQ(StringUtil::ToHex(value), '6');
560+
561+
value = 10;
562+
ASSERT_EQ(StringUtil::ToHex(value), 'A');
563+
564+
value = 15;
565+
ASSERT_EQ(StringUtil::ToHex(value), 'F');
566+
}
567+
568+
TEST(TestStringUtil, FromHex) {
569+
char c = '6';
570+
ASSERT_EQ(StringUtil::FromHex(c), 6);
571+
572+
c = 'A';
573+
ASSERT_EQ(StringUtil::FromHex(c), 10);
574+
575+
c = 'F';
576+
ASSERT_EQ(StringUtil::FromHex(c), 15);
577+
}
578+
579+
TEST(TestStringUtil, Quote) {
580+
string rawStr = "";
581+
ASSERT_EQ(StringUtil::Quote(rawStr), "");
582+
583+
rawStr = "When using MacPorts GCC on OS X and enlisting the Clang Integrated Assembler via -Wa,-q, the assembler produces a stream of warnings for each file. A sampling of the warnings is shown below (so many of them, the Stack Overflow editor would not allow me to paste the entire stream).";
584+
/*When%20using%20MacPorts%20GCC%20on%20OS%20X%20and%20enlisting%20the%20Clang%20Integrated%20Assembler%20via%20-Wa,-q,%20the%20assembler%20produces%20a%20stream%20of%20warnings%20for%20each%20file.%20A%20sampling%20of%20the%20warnings%20is%20shown%20below%20%28so%20many%20of%20them,%20the%20Stack%20Overflow%20editor%20would%20not%20allow%20me%20to%20paste%20the%20entire%20stream%29.* */
585+
cout << StringUtil::Quote(rawStr) << endl;
586+
}
587+
588+
TEST(TestStringUtil, UnQuote) {
589+
string quoteStr = "";
590+
ASSERT_EQ(StringUtil::UnQuote(quoteStr), "");
591+
592+
//rawStr = "When using MacPorts GCC on OS X and enlisting the Clang Integrated Assembler via -Wa,-q, the assembler produces a stream of warnings for each file. A sampling of the warnings is shown below (so many of them, the Stack Overflow editor would not allow me to paste the entire stream).";
593+
quoteStr = "When%20using%20MacPorts%20GCC%20on%20OS%20X%20and%20enlisting%20the%20Clang%20Integrated%20Assembler%20via%20-Wa,-q,%20the%20assembler%20produces%20a%20stream%20of%20warnings%20for%20each%20file.%20A%20sampling%20of%20the%20warnings%20is%20shown%20below%20%28so%20many%20of%20them,%20the%20Stack%20Overflow%20editor%20would%20not%20allow%20me%20to%20paste%20the%20entire%20stream%29.";
594+
cout << StringUtil::UnQuote(quoteStr) << endl;
595+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test 2

0 commit comments

Comments
 (0)