Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#include "quotes.h" | ||
|
||
using namespace std; | ||
|
||
string quote( string & unquoted_string) | ||
{ | ||
string dest = "'"; | ||
|
||
for( string::iterator sit = unquoted_string.begin(); sit < unquoted_string.end(); sit++) | ||
if( *sit == '\'') | ||
dest += "'\\''"; | ||
else | ||
dest += *sit; | ||
|
||
dest += "'"; | ||
|
||
return dest; | ||
} | ||
|
||
// helper | ||
// preconditions: | ||
// - starts with ['"] | ||
// - as a consequence: size > 0 | ||
bool parse_quoted_string( string::iterator & sit, string::iterator last, string & ret) | ||
{ | ||
char quote_char = *( sit++); | ||
|
||
if( quote_char == '"') | ||
{ | ||
while( sit < last) | ||
{ | ||
switch( *sit) | ||
{ | ||
case '"': | ||
return true; | ||
|
||
case '\\': | ||
switch( *(++sit)) | ||
{ | ||
case '"': | ||
case '\\': | ||
case '`': | ||
case '$': | ||
case '\n': | ||
ret += *(sit++); | ||
break; | ||
|
||
default: | ||
ret += '\\'; | ||
break; | ||
} | ||
break; | ||
|
||
default: | ||
ret += *(sit++); | ||
break; | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
while( sit < last) | ||
{ | ||
if( *sit == '\'') | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
ret += *(sit++); | ||
} | ||
} | ||
} | ||
|
||
// missing closing quote | ||
return false; | ||
} | ||
|
||
string unquote( string & quoted_string) | ||
{ | ||
string res; | ||
string::iterator sit = quoted_string.begin(); | ||
|
||
while( sit < quoted_string.end()) | ||
{ | ||
while( sit < quoted_string.end() && | ||
*sit != '"' && | ||
*sit != '\'') | ||
{ | ||
if( *sit == '\\') | ||
{ | ||
if( *(++sit) != '\n') | ||
res += *sit; | ||
} | ||
else | ||
res += *sit; | ||
|
||
sit++; | ||
} | ||
|
||
if( !( sit < quoted_string.end())) | ||
break; | ||
|
||
string substr; | ||
if( parse_quoted_string( sit, quoted_string.end(), substr)) | ||
res += substr; | ||
sit++; | ||
} | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#ifndef __QUOTES_H__ | ||
#define __QUOTES_H__ | ||
|
||
#include <string> | ||
|
||
std::string quote( std::string & unquoted_string); | ||
std::string unquote( std::string & unquoted_string); | ||
|
||
#endif |