mapleFU commented on code in PR #2873:
URL: https://github.com/apache/kvrocks/pull/2873#discussion_r2041130068
##########
src/common/string_util.cc:
##########
@@ -276,6 +276,71 @@ std::pair<std::string, std::string>
SplitGlob(std::string_view glob) {
return {prefix, ""};
}
+StatusOr<std::vector<std::string>> SplitArguments(std::string_view in) {
Review Comment:
Can we add a comment that currently `\xHH` (hex) is not supportted?
##########
src/common/string_util.cc:
##########
@@ -276,6 +276,71 @@ std::pair<std::string, std::string>
SplitGlob(std::string_view glob) {
return {prefix, ""};
}
+StatusOr<std::vector<std::string>> SplitArguments(std::string_view in) {
+ std::vector<std::string> arguments;
+ std::string current_string;
+
+ enum State { NORMAL, DOUBLE_QUOTED, SINGLE_QUOTED, ESCAPE } state = NORMAL;
+
+ State state_before_escape = NORMAL;
+ for (const char c : in) {
+ switch (state) {
+ case NORMAL:
+ if (std::isspace(c)) {
+ if (!current_string.empty()) {
+ arguments.emplace_back(std::move(current_string));
+ current_string.clear();
+ }
+ // skip spaces
+ } else if (c == '"' || c == '\'') {
+ state = c == '"' ? DOUBLE_QUOTED : SINGLE_QUOTED;
+ } else {
+ current_string.push_back(c);
+ }
+ break;
+ case DOUBLE_QUOTED:
+ case SINGLE_QUOTED:
+ if (c == '\\') {
+ state_before_escape = state;
+ state = ESCAPE;
+ } else if ((c == '"' && state == DOUBLE_QUOTED) || (c == '\'' && state
== SINGLE_QUOTED)) {
+ state = NORMAL;
+ } else {
+ current_string.push_back(c);
+ }
+ break;
+ case ESCAPE:
+ if (c == '"' || c == '\'' || c == '\\') {
+ current_string.push_back(c);
+ } else if (c == 't') {
+ current_string.push_back('\t');
+ } else if (c == 'r') {
+ current_string.push_back('\r');
+ } else if (c == 'n') {
+ current_string.push_back('\n');
+ } else if (c == 'v') {
+ current_string.push_back('\v');
+ } else if (c == 'f') {
+ current_string.push_back('\f');
+ } else if (c == 'b') {
+ current_string.push_back('\b');
+ }
Review Comment:
The redis code is:
```c
switch(*p) {
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'b': c = '\b'; break;
case 'a': c = '\a'; break;
default: c = *p; break;
}
```
Should we `push_back(c)` after checks above?
##########
src/common/string_util.cc:
##########
@@ -276,6 +276,71 @@ std::pair<std::string, std::string>
SplitGlob(std::string_view glob) {
return {prefix, ""};
}
+StatusOr<std::vector<std::string>> SplitArguments(std::string_view in) {
+ std::vector<std::string> arguments;
+ std::string current_string;
+
+ enum State { NORMAL, DOUBLE_QUOTED, SINGLE_QUOTED, ESCAPE } state = NORMAL;
+
+ State state_before_escape = NORMAL;
+ for (const char c : in) {
+ switch (state) {
+ case NORMAL:
+ if (std::isspace(c)) {
+ if (!current_string.empty()) {
+ arguments.emplace_back(std::move(current_string));
+ current_string.clear();
+ }
+ // skip spaces
+ } else if (c == '"' || c == '\'') {
+ state = c == '"' ? DOUBLE_QUOTED : SINGLE_QUOTED;
+ } else {
+ current_string.push_back(c);
+ }
+ break;
+ case DOUBLE_QUOTED:
+ case SINGLE_QUOTED:
+ if (c == '\\') {
+ state_before_escape = state;
+ state = ESCAPE;
+ } else if ((c == '"' && state == DOUBLE_QUOTED) || (c == '\'' && state
== SINGLE_QUOTED)) {
+ state = NORMAL;
Review Comment:
should we checks that:
```
/* closing quote must be followed by a space or
* nothing at all. */
```
##########
src/common/string_util.cc:
##########
@@ -276,6 +276,71 @@ std::pair<std::string, std::string>
SplitGlob(std::string_view glob) {
return {prefix, ""};
}
+StatusOr<std::vector<std::string>> SplitArguments(std::string_view in) {
+ std::vector<std::string> arguments;
+ std::string current_string;
+
+ enum State { NORMAL, DOUBLE_QUOTED, SINGLE_QUOTED, ESCAPE } state = NORMAL;
+
+ State state_before_escape = NORMAL;
+ for (const char c : in) {
+ switch (state) {
+ case NORMAL:
+ if (std::isspace(c)) {
Review Comment:
```
switch(*p) {
case ' ':
case '\n':
case '\r':
case '\t':
case '\0':
```
should all of these being support?
##########
src/common/string_util.cc:
##########
@@ -276,6 +276,71 @@ std::pair<std::string, std::string>
SplitGlob(std::string_view glob) {
return {prefix, ""};
}
+StatusOr<std::vector<std::string>> SplitArguments(std::string_view in) {
+ std::vector<std::string> arguments;
+ std::string current_string;
+
+ enum State { NORMAL, DOUBLE_QUOTED, SINGLE_QUOTED, ESCAPE } state = NORMAL;
+
+ State state_before_escape = NORMAL;
+ for (const char c : in) {
+ switch (state) {
+ case NORMAL:
+ if (std::isspace(c)) {
+ if (!current_string.empty()) {
+ arguments.emplace_back(std::move(current_string));
+ current_string.clear();
+ }
+ // skip spaces
+ } else if (c == '"' || c == '\'') {
+ state = c == '"' ? DOUBLE_QUOTED : SINGLE_QUOTED;
+ } else {
+ current_string.push_back(c);
+ }
+ break;
+ case DOUBLE_QUOTED:
+ case SINGLE_QUOTED:
+ if (c == '\\') {
+ state_before_escape = state;
+ state = ESCAPE;
+ } else if ((c == '"' && state == DOUBLE_QUOTED) || (c == '\'' && state
== SINGLE_QUOTED)) {
+ state = NORMAL;
+ } else {
+ current_string.push_back(c);
+ }
+ break;
+ case ESCAPE:
+ if (c == '"' || c == '\'' || c == '\\') {
+ current_string.push_back(c);
+ } else if (c == 't') {
Review Comment:
I found that redis doesn't check these in single quote mode but I don't
understand why 😅
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]