]> mj.ucw.cz Git - libucw.git/commitdiff
Simple parser for escape sequences.
authorPavel Charvat <pavel.charvat@netcentrum.cz>
Mon, 24 Apr 2006 11:56:13 +0000 (13:56 +0200)
committerPavel Charvat <pavel.charvat@netcentrum.cz>
Mon, 24 Apr 2006 11:56:13 +0000 (13:56 +0200)
lib/Makefile
lib/lib.h
lib/stkstring.h
lib/string.c [new file with mode: 0644]

index e4126c7fefe369257247c7eacb52ed8df108c230..216da3c1f17fe09edac5854e193d9e7f9f16742b 100644 (file)
@@ -26,7 +26,8 @@ LIBUCW_MODS= \
        md5 md5hex \
        base64 base224 \
        sync \
-       qache
+       qache \
+       string
 
 LIBUCW_INCLUDES= \
        lib.h config.h math.h \
index 06a42ecec20fabae00dfab0915265c03787d8147..9f7f59d4ddf58f8467e1b1b38854626ac56496f2 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -250,4 +250,8 @@ struct sigaction;
 void handle_signal(int signum, struct sigaction *oldact);
 void unhandle_signal(int signum, struct sigaction *oldact);
 
+/* string.c */
+
+byte *str_unesc(byte *dest, byte *src);
+
 #endif
index 8f58aec736b87012c1990fde567e118de8e6c348..22808cc78057b3a336d457dfbc363d5720b501b5 100644 (file)
@@ -18,6 +18,7 @@
 #define stk_strarraycat(s,n) ({ char **_s=(s); int _n=(n); char *_x=alloca(stk_array_len(_s,_n)); stk_array_copy(_x, _s, _n); _x; })
 #define stk_printf(f...) ({ uns _l=stk_printf_internal(f); char *_x=alloca(_l); memcpy(_x, stk_printf_buf, _l); _x; })
 #define stk_hexdump(s,n) ({ uns _n=(n); char *_x=alloca(3*_n+1); stk_hexdump_internal(_x,(byte*)(s),_n); _x; })
+#define stk_str_unesc(s) ({ byte *_s=(s); byte *_d=alloca(strlen(_s)+1); str_unesc(_d, _s); _d; })
 
 uns stk_array_len(char **s, uns cnt);
 void stk_array_copy(char *x, char **s, uns cnt);
diff --git a/lib/string.c b/lib/string.c
new file mode 100644 (file)
index 0000000..01685f8
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ *     UCW Library -- String Routines
+ *
+ *     (c) 2006 Pavel Charvat <pchar@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#include "lib/lib.h"
+#include "lib/chartype.h"
+
+/* Expands C99-like escape sequences.
+ * It is safe to use the same buffer for both input and output. */
+byte *
+str_unesc(byte *d, byte *s)
+{
+  while (*s)
+    {
+      if (*s == '\\')
+       switch (s[1])
+         {
+           case 'a': *d++ = '\a'; s += 2; break;
+           case 'b': *d++ = '\b'; s += 2; break;
+           case 'f': *d++ = '\f'; s += 2; break;
+           case 'n': *d++ = '\n'; s += 2; break;
+           case 'r': *d++ = '\r'; s += 2; break;
+           case 't': *d++ = '\t'; s += 2; break;
+           case 'v': *d++ = '\v'; s += 2; break;
+           case '\?': *d++ = '\?'; s += 2; break;
+           case '\'': *d++ = '\''; s += 2; break;
+           case '\"': *d++ = '\"'; s += 2; break;
+           case '\\': *d++ = '\\'; s += 2; break;
+           case '0':
+             if (s[2] < '0' || s[2] > '7')
+               *d++ = *s++;
+             else
+               {
+                 uns v = 0;
+                 for (s += 2; *s >= '0' && *s <= '7' && v < 32; s++)
+                   v = (v << 3) + *s - '0';
+                 *d++ = v;
+               }
+             break;
+           case 'x':
+             if (!Cxdigit(s[2]))
+               *d++ = *s++;
+             else
+               {
+                 uns v = 0;
+                 for (s += 2; Cxdigit(*s) && v < 16; s++)
+                   v = (v << 4) + (Cdigit(*s) ? (*s - '0') : ((*s | 32) - 'A' + 10));
+                 *d++ = v;
+               }
+             break;
+            default:
+             *d++ = *s++;
+             break;
+         }
+      else
+       *d++ = *s++;
+    }
+  *d = 0;
+  return d;
+}
+