Message ID | 20161103040734.8098-2-quwenruo@cn.fujitsu.com (mailing list archive) |
---|---|
State | Accepted |
Headers | show |
On Thu, Nov 03, 2016 at 12:07:31PM +0800, Qu Wenruo wrote: > Introduce new function, escape_string_inplace(), to escape specified > characters in place. > > Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> > --- > utils.c | 24 ++++++++++++++++++++++++ > utils.h | 14 ++++++++++++++ > 2 files changed, 38 insertions(+) > > diff --git a/utils.c b/utils.c > index 3f54245..3c50d84 100644 > --- a/utils.c > +++ b/utils.c > @@ -4251,3 +4251,27 @@ unsigned int rand_range(unsigned int upper) > */ > return (unsigned int)(jrand48(rand_seed) % upper); > } > + > +static void escape_one_char(char *restrict string, char escape) > +{ > + int i = 0; > + int j = 0; > + > + while (string[j] != '\0') { > + if (string[j] != escape) { > + string[i] = string[j]; > + i++; > + } So how does this actually escape the characters? I don't see anything inserted. I think we don't have a common understanding of what's supposed to be done here. I mean C-string-like escaping. And this cannot be done inplace, as it increases string length. I've implemented it in a different way, so this patch is left out. The final stream dump output is preserved. -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
At 11/07/2016 06:20 PM, David Sterba wrote: > On Thu, Nov 03, 2016 at 12:07:31PM +0800, Qu Wenruo wrote: >> Introduce new function, escape_string_inplace(), to escape specified >> characters in place. >> >> Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> >> --- >> utils.c | 24 ++++++++++++++++++++++++ >> utils.h | 14 ++++++++++++++ >> 2 files changed, 38 insertions(+) >> >> diff --git a/utils.c b/utils.c >> index 3f54245..3c50d84 100644 >> --- a/utils.c >> +++ b/utils.c >> @@ -4251,3 +4251,27 @@ unsigned int rand_range(unsigned int upper) >> */ >> return (unsigned int)(jrand48(rand_seed) % upper); >> } >> + >> +static void escape_one_char(char *restrict string, char escape) >> +{ >> + int i = 0; >> + int j = 0; >> + >> + while (string[j] != '\0') { >> + if (string[j] != escape) { >> + string[i] = string[j]; >> + i++; >> + } > > So how does this actually escape the characters? I don't see anything > inserted. I think we don't have a common understanding of what's > supposed to be done here. I mean C-string-like escaping. And this cannot > be done inplace, as it increases string length. I've implemented it in a > different way, so this patch is left out. The final stream dump output > is preserved. > > Oh, sorry. I misunderstand the term "string escaping". I though it's just deleting special characters. I checked your print_path_escaped() and finally got what string-escaping should do. Sorry for the extra trouble. While still some small comment. + if (!isprint(c)) { + printf("\\%c%c%c", + '0' + ((c & 0300) >> 6), + '0' + ((c & 070) >> 3), + '0' + (c & 07)); For non-printable chars, isn't it more common to print it as hex other than octal? Thanks, Qu -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
At 11/07/2016 06:20 PM, David Sterba wrote: > On Thu, Nov 03, 2016 at 12:07:31PM +0800, Qu Wenruo wrote: >> Introduce new function, escape_string_inplace(), to escape specified >> characters in place. >> >> Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> >> --- >> utils.c | 24 ++++++++++++++++++++++++ >> utils.h | 14 ++++++++++++++ >> 2 files changed, 38 insertions(+) >> >> diff --git a/utils.c b/utils.c >> index 3f54245..3c50d84 100644 >> --- a/utils.c >> +++ b/utils.c >> @@ -4251,3 +4251,27 @@ unsigned int rand_range(unsigned int upper) >> */ >> return (unsigned int)(jrand48(rand_seed) % upper); >> } >> + >> +static void escape_one_char(char *restrict string, char escape) >> +{ >> + int i = 0; >> + int j = 0; >> + >> + while (string[j] != '\0') { >> + if (string[j] != escape) { >> + string[i] = string[j]; >> + i++; >> + } > > So how does this actually escape the characters? I don't see anything > inserted. I think we don't have a common understanding of what's > supposed to be done here. I mean C-string-like escaping. And this cannot > be done inplace, as it increases string length. I've implemented it in a > different way, so this patch is left out. The final stream dump output > is preserved. > > And further more. The new string escaping is screwing up original output. Operation lile mkfile lacks the final '\n'. String over the width will lack the ending ' ' to seperate later key=value pairs. It would be better to output the escaped string into a buffer and still use printf() format to output them out as it used to be. Thanks, Qu -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On Tue, Nov 08, 2016 at 08:33:25AM +0800, Qu Wenruo wrote: > While still some small comment. > + if (!isprint(c)) { > + printf("\\%c%c%c", > + '0' + ((c & 0300) >> 6), > + '0' + ((c & 070) >> 3), > + '0' + (c & 07)); > > For non-printable chars, isn't it more common to print it as hex other > than octal? Ok, hex would be better. -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
On Tue, Nov 08, 2016 at 08:57:52AM +0800, Qu Wenruo wrote: > And further more. > > The new string escaping is screwing up original output. > > Operation lile mkfile lacks the final '\n'. > String over the width will lack the ending ' ' to seperate later > key=value pairs. Oh right, that's the shortcut if format is null, this should print the '\n' as welll, I'll fix it. > It would be better to output the escaped string into a buffer and still > use printf() format to output them out as it used to be. I want to avoid using the intermediate string buffers unless it's really necessary. -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
diff --git a/utils.c b/utils.c index 3f54245..3c50d84 100644 --- a/utils.c +++ b/utils.c @@ -4251,3 +4251,27 @@ unsigned int rand_range(unsigned int upper) */ return (unsigned int)(jrand48(rand_seed) % upper); } + +static void escape_one_char(char *restrict string, char escape) +{ + int i = 0; + int j = 0; + + while (string[j] != '\0') { + if (string[j] != escape) { + string[i] = string[j]; + i++; + } + j++; + } + /* pend the finishing '\0' */ + string[i] = '\0'; +} + +void string_escape_inplace(char *restrict string, char *restrict escape_chars) +{ + int i; + + for (i = 0; i < strlen(escape_chars); i++) + escape_one_char(string, escape_chars[i]); +} diff --git a/utils.h b/utils.h index 1a2dbcd..dae8db4 100644 --- a/utils.h +++ b/utils.h @@ -457,4 +457,18 @@ unsigned int rand_range(unsigned int upper); /* Also allow setting the seed manually */ void init_rand_seed(u64 seed); +void string_escape_inplace(char *restrict string, char *restrict escape_chars); + +static inline char* string_escape(char *restrict src, + char *restrict escape_chars) +{ + char *ret; + + ret = malloc(strlen(src) + 1); + if (!ret) + return NULL; + string_escape_inplace(ret, escape_chars); + return ret; +} + #endif
Introduce new function, escape_string_inplace(), to escape specified characters in place. Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> --- utils.c | 24 ++++++++++++++++++++++++ utils.h | 14 ++++++++++++++ 2 files changed, 38 insertions(+)