diff options
| author | h2so5 <[email protected]> | 2013-04-10 23:32:51 +0900 |
|---|---|---|
| committer | h2so5 <[email protected]> | 2013-04-10 23:32:51 +0900 |
| commit | a4d59b4ceb3a30d7665edb8d674d85c9d8b874d8 (patch) | |
| tree | 2dff88639de89ec81bdafbabc54ae6121c9661c4 /src | |
| parent | 887339dc199829c423f58190d65492399a2d4aa9 (diff) | |
| download | mruby-a4d59b4ceb3a30d7665edb8d674d85c9d8b874d8.tar.gz mruby-a4d59b4ceb3a30d7665edb8d674d85c9d8b874d8.zip | |
Refactor mrb_str_dump
Diffstat (limited to 'src')
| -rw-r--r-- | src/string.c | 200 |
1 files changed, 107 insertions, 93 deletions
diff --git a/src/string.c b/src/string.c index 329a3da9d..133b023fc 100644 --- a/src/string.c +++ b/src/string.c @@ -2252,105 +2252,119 @@ mrb_str_upcase(mrb_state *mrb, mrb_value self) mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str) { - mrb_int len; - const char *p, *pend; - char *q; - struct RString *result; - - len = 2; /* "" */ - p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str); - while (p < pend) { - unsigned char c = *p++; - switch (c) { - case '"': case '\\': - case '\n': case '\r': - case '\t': case '\f': - case '\013': case '\010': case '\007': case '\033': - len += 2; - break; - - case '#': - len += IS_EVSTR(p, pend) ? 2 : 1; - break; + mrb_int len; + const char *p, *pend; + char *q; + struct RString *result; - default: - if (ISPRINT(c)) { - len++; - } - else { - len += 4; /* \NNN */ - } - break; - } + len = 2; /* "" */ + p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str); + while (p < pend) { + unsigned char c = *p++; + switch (c) { + case '"': case '\\': + case '\n': case '\r': + case '\t': case '\f': + case '\013': case '\010': case '\007': case '\033': + len += 2; + break; + + case '#': + len += IS_EVSTR(p, pend) ? 2 : 1; + break; + + default: + if (ISPRINT(c)) { + len++; + } + else { + len += 4; /* \NNN */ + } + break; } + } - result = str_new(mrb, 0, len); - str_with_class(mrb, result, str); - p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str); - q = result->ptr; - - *q++ = '"'; - while (p < pend) { - unsigned char c = *p++; + result = str_new(mrb, 0, len); + str_with_class(mrb, result, str); + p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str); + q = result->ptr; - if (c == '"' || c == '\\') { - *q++ = '\\'; - *q++ = c; - } - else if (c == '#') { - if (IS_EVSTR(p, pend)) *q++ = '\\'; - *q++ = '#'; - } - else if (c == '\n') { - *q++ = '\\'; - *q++ = 'n'; - } - else if (c == '\r') { - *q++ = '\\'; - *q++ = 'r'; - } - else if (c == '\t') { - *q++ = '\\'; - *q++ = 't'; - } - else if (c == '\f') { - *q++ = '\\'; - *q++ = 'f'; - } - else if (c == '\013') { - *q++ = '\\'; - *q++ = 'v'; - } - else if (c == '\010') { - *q++ = '\\'; - *q++ = 'b'; - } - else if (c == '\007') { - *q++ = '\\'; - *q++ = 'a'; - } - else if (c == '\033') { - *q++ = '\\'; - *q++ = 'e'; - } - else if (ISPRINT(c)) { + *q++ = '"'; + while (p < pend) { + unsigned char c = *p++; + + switch (c) { + case '"': + case '\\': + *q++ = '\\'; + *q++ = c; + break; + + case '\n': + *q++ = '\\'; + *q++ = 'n'; + break; + + case '\r': + *q++ = '\\'; + *q++ = 'r'; + break; + + case '\t': + *q++ = '\\'; + *q++ = 't'; + break; + + case '\f': + *q++ = '\\'; + *q++ = 'f'; + break; + + case '\013': + *q++ = '\\'; + *q++ = 'v'; + break; + + case '\010': + *q++ = '\\'; + *q++ = 'b'; + break; + + case '\007': + *q++ = '\\'; + *q++ = 'a'; + break; + + case '\033': + *q++ = '\\'; + *q++ = 'e'; + break; + + case '#': + if (IS_EVSTR(p, pend)) *q++ = '\\'; + *q++ = '#'; + break; + + default: + if (ISPRINT(c)) { *q++ = c; - } - else { - mrb_value octstr; - mrb_value chr; - const char *ptr; - int len; - chr = mrb_fixnum_value(c & 0xff); - octstr = mrb_fixnum_to_str(mrb, chr, 8); - ptr = mrb_str_body(octstr, &len); - memcpy(q, "\\000", 4); - memcpy(q + 4 - len, ptr, len); - q += 4; - } + } + else { + mrb_value octstr; + mrb_value chr; + const char *ptr; + int len; + chr = mrb_fixnum_value(c & 0xff); + octstr = mrb_fixnum_to_str(mrb, chr, 8); + ptr = mrb_str_body(octstr, &len); + memcpy(q, "\\000", 4); + memcpy(q + 4 - len, ptr, len); + q += 4; + } } - *q++ = '"'; - return mrb_obj_value(result); + } + *q++ = '"'; + return mrb_obj_value(result); } mrb_value |
