aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-01-17 23:06:37 -0800
committerBruce Hill <bruce@bruce-hill.com>2021-01-17 23:06:37 -0800
commitffdf32da2f20d21d5adee1231e3ac8207edab78d (patch)
tree25f05c94ab280d85b5934cc6607909a35c8c22fb
parentff597be5fc52d1d8bd8050fc81050308f76c9b05 (diff)
Code cleanup on utils
-rw-r--r--pattern.c6
-rw-r--r--print.c2
-rw-r--r--utils.c57
-rw-r--r--utils.h4
4 files changed, 16 insertions, 53 deletions
diff --git a/pattern.c b/pattern.c
index b378bc3..94643f9 100644
--- a/pattern.c
+++ b/pattern.c
@@ -284,11 +284,11 @@ static pat_t *_bp_simplepattern(file_t *f, const char *str)
pat_t *esc;
const char *opstart = str;
- unsigned char e = unescapechar(str, &str);
+ unsigned char e = (unsigned char)unescapechar(str, &str);
if (*str == '-') { // Escape range (e.g. \x00-\xFF)
++str;
const char *seqstart = str;
- unsigned char e2 = unescapechar(str, &str);
+ unsigned char e2 = (unsigned char)unescapechar(str, &str);
if (str == seqstart)
file_err(f, seqstart, str+1, "This value isn't a valid escape sequence");
if (e2 < e)
@@ -516,7 +516,7 @@ pat_t *bp_stringpattern(file_t *f, const char *str)
}
const char *after_escape;
- unsigned char e = unescapechar(&str[1], &after_escape);
+ char e = unescapechar(&str[1], &after_escape);
// If there is not a special escape sequence (\n, \x0A, etc.)
// or \\, \", \', \`, then check for an interpolated value:
// The special cases for single and double quotes aren't
diff --git a/print.c b/print.c
index c5ba432..b21a3ea 100644
--- a/print.c
+++ b/print.c
@@ -297,7 +297,7 @@ void _print_match(FILE *out, printer_t *pr, match_t *m)
if (*r == '\\') {
++r;
- unsigned char c = unescapechar(r, &r);
+ char c = unescapechar(r, &r);
fputc(c, out);
if (c == '\n') {
++line;
diff --git a/utils.c b/utils.c
index 3c38e4e..76651bc 100644
--- a/utils.c
+++ b/utils.c
@@ -56,9 +56,8 @@ int matchchar(const char **str, char c)
if (*next == c) {
*str = &next[1];
return 1;
- } else {
- return 0;
}
+ return 0;
}
//
@@ -70,9 +69,8 @@ int matchstr(const char **str, const char *target)
if (strncmp(next, target, strlen(target)) == 0) {
*str = &next[strlen(target)];
return 1;
- } else {
- return 0;
}
+ return 0;
}
//
@@ -80,7 +78,7 @@ int matchstr(const char **str, const char *target)
// character that was escaped.
// Set *end = the first character past the end of the escape sequence.
//
-unsigned char unescapechar(const char *escaped, const char **end)
+char unescapechar(const char *escaped, const char **end)
{
size_t len = 1;
unsigned char ret = (unsigned char)*escaped;
@@ -117,7 +115,7 @@ unsigned char unescapechar(const char *escaped, const char **end)
default: break;
}
*end = &escaped[len];
- return ret;
+ return (char)ret;
}
//
@@ -127,50 +125,17 @@ unsigned char unescapechar(const char *escaped, const char **end)
size_t unescape_string(char *dest, const char *src, size_t bufsize)
{
size_t len = 0;
-#define PUT(c) do { *(dest++) = (char)(c); ++len; } while (0)
- for ( ; *src && len < bufsize; ++src) {
- if (*src != '\\') {
- PUT(*src);
- continue;
- }
- ++src;
- switch (*src) {
- case 'a': PUT('\a'); break; case 'b': PUT('\b'); break;
- case 'n': PUT('\n'); break; case 'r': PUT('\r'); break;
- case 't': PUT('\t'); break; case 'v': PUT('\v'); break;
- case 'e': PUT('\033'); break;
- case 'x': { // Hex
- static const char hextable[255] = {
- ['0']=0x10, ['1']=0x1, ['2']=0x2, ['3']=0x3, ['4']=0x4,
- ['5']=0x5, ['6']=0x6, ['7']=0x7, ['8']=0x8, ['9']=0x9,
- ['a']=0xa, ['b']=0xb, ['c']=0xc, ['d']=0xd, ['e']=0xe, ['f']=0xf,
- ['A']=0xa, ['B']=0xb, ['C']=0xc, ['D']=0xd, ['E']=0xe, ['F']=0xf,
- };
- if (hextable[(int)src[1]] && hextable[(int)src[2]]) {
- PUT((hextable[(int)src[1]] << 4) | (hextable[(int)src[2]] & 0xF));
- src += 2;
- }
- break;
- }
- case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': { // Octal
- int c = *src - '0';
- if ('0' <= src[1] && src[1] <= '7') {
- ++src;
- c = (c << 3) | (*src - '0');
- if ('0' <= src[1] && src[1] <= '7' && (c << 3) < 256) {
- ++src;
- c = (c << 3) | (*src - '0');
- }
- }
- PUT(c);
- break;
- }
- default: PUT(*src); break;
+ while (*src && len < bufsize) {
+ if (*src == '\\') {
+ ++src;
+ *(dest++) = unescapechar(src, &src);
+ } else {
+ *(dest++) = *(src++);
}
+ ++len;
}
*dest = '\0';
return len;
-#undef PUT
}
//
diff --git a/utils.h b/utils.h
index b6aece3..ef0e311 100644
--- a/utils.h
+++ b/utils.h
@@ -11,15 +11,13 @@
#include "match.h"
#define streq(a, b) (strcmp(a, b) == 0)
-// TODO: better error reporting
#define check(cond, ...) do { if (!(cond)) { fprintf(stderr, __VA_ARGS__); fwrite("\n", 1, 1, stderr); exit(1); } } while(0)
-#define debug(...) do { if (verbose) fprintf(stderr, __VA_ARGS__); } while(0)
#define new(t) memcheck(calloc(sizeof(t), 1))
#define xcalloc(a,b) memcheck(calloc(a,b))
#define xrealloc(a,b) memcheck(realloc(a,b))
__attribute__((nonnull))
-unsigned char unescapechar(const char *escaped, const char **end);
+char unescapechar(const char *escaped, const char **end);
__attribute__((pure, nonnull))
const char *after_name(const char *str);
__attribute__((pure, nonnull, returns_nonnull))