[Z88dk-commits] CVS: z88dk/src/z80asm/lib strutil.c, 1.10, 1.11 stru

Bridge to the z88dk-commits mailing list
Post Reply
pauloscustodio

[Z88dk-commits] CVS: z88dk/src/z80asm/lib strutil.c, 1.10, 1.11 stru

Post by pauloscustodio »

Update of /cvsroot/z88dk/z88dk/src/z80asm/lib
In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv1021

Modified Files:
strutil.c strutil.h
Log Message:
Add str_compress_escapes() to compress C-like escape sequences.
Accepts \a, \b, \e, \f, \n, \r, \t, \v, \xhh, \{any} \ooo, allows \0 in the string.

Index: strutil.c
===================================================================
RCS file: /cvsroot/z88dk/z88dk/src/z80asm/lib/strutil.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** strutil.c 19 Mar 2014 23:04:57 -0000 1.10
--- strutil.c 29 Mar 2014 22:04:11 -0000 1.11
***************
*** 99,102 ****
--- 99,172 ----
}

+ static int char_digit(char c)
+ {
+ if (isdigit(c)) return c - '0';
+ if (isxdigit(c)) return 10 + toupper(c) - 'A';
+ return -1;
+ }
+
+ /* convert C-escape sequences - modify in place, return final length
+ to allow strings with '\0' characters
+ Accepts \a, \b, \e, \f, \n, \r, \t, \v, \xhh, \{any} \ooo
+ code borrowed from GLib */
+ uint_t str_compress_escapes( char *string )
+ {
+ char *p, *q, *num;
+ int base = 0, max_digits, digit;
+
+ for ( p = q = string; *p; p++ )
+ {
+ if (*p == '\\')
+ {
+ p++;
+ base = 0; /* decision octal/hex */
+ switch (*p)
+ {
+ case '\0': p--; break; /* trailing backslash - ignore */
+ case 'a': *q++ = '\a'; break;
+ case 'b': *q++ = '\b'; break;
+ case 'e': *q++ = '\x1B'; break;
+ case 'f': *q++ = '\f'; break;
+ case 'n': *q++ = '\n'; break;
+ case 'r': *q++ = '\r'; break;
+ case 't': *q++ = '\t'; break;
+ case 'v': *q++ = '\v'; break;
+ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7':
+ num = p; /* start of number */
+ base = 8;
+ max_digits = 3;
+ /* fall through */
+ case 'x':
+ if ( base == 0 ) /* not octal */
+ {
+ num = ++p;
+ base = 16;
+ max_digits = 2;
+ }
+ /* convert octal or hex number */
+ *q = 0;
+ while ( p < num + max_digits &&
+ (digit = char_digit(*p)) >= 0 &&
+ digit < base )
+ {
+ *q = *q * base + digit;
+ p++;
+ }
+ p--;
+ q++;
+ break;
+ default: *q++ = *p; /* any other char */
+ }
+ }
+ else
+ {
+ *q++ = *p;
+ }
+ }
+ *q = '\0';
+
+ return q - string;
+ }
+
/*-----------------------------------------------------------------------------
* String class
***************
*** 178,181 ****
--- 248,257 ----
}

+ void Str_compress_escapes( Str *self )
+ {
+ /* no Str_sync_len() as string may have '\0' */
+ self->len = str_compress_escapes( self->str );
+ }
+
/*-----------------------------------------------------------------------------
* expand if needed to store at least more num_chars plus a zero byte
***************
*** 428,431 ****
--- 504,511 ----
/*
* $Log$
+ * Revision 1.11 2014/03/29 22:04:11 pauloscustodio
+ * Add str_compress_escapes() to compress C-like escape sequences.
+ * Accepts \a, \b, \e, \f, \n, \r, \t, \v, \xhh, \{any} \ooo, allows \0 in the string.
+ *
* Revision 1.10 2014/03/19 23:04:57 pauloscustodio
* Add Str_set_alias() to define an alias char* that always points to self->str

Index: strutil.h
===================================================================
RCS file: /cvsroot/z88dk/z88dk/src/z80asm/lib/strutil.h,v
retrieving revision 1.12
retrieving revision 1.13
diff -C2 -d -r1.12 -r1.13
*** strutil.h 19 Mar 2014 23:04:57 -0000 1.12
--- strutil.h 29 Mar 2014 22:04:11 -0000 1.13
***************
*** 33,36 ****
--- 33,41 ----
extern char *strip( char *string );

+ /* convert C-escape sequences - modify in place, return final length
+ to allow strings with '\0' characters
+ Accepts \b, \f, \n, \r, \t, \v, \xhh, \? \ooo */
+ extern uint_t str_compress_escapes( char *string );
+
/*-----------------------------------------------------------------------------
* String class - dual use
***************
*** 109,115 ****
extern void Str_append_vsprintf( Str *self, char *format, va_list argptr );

! /* chomp, strip */
extern void Str_chomp( Str *self );
extern void Str_strip( Str *self );

/* get N characters from input, return FALSE on EOF */
--- 114,121 ----
extern void Str_append_vsprintf( Str *self, char *format, va_list argptr );

! /* chomp, strip, compress_escapes */
extern void Str_chomp( Str *self );
extern void Str_strip( Str *self );
+ extern void Str_compress_escapes( Str *self );

/* get N characters from input, return FALSE on EOF */
***************
*** 124,127 ****
--- 130,137 ----
/*
* $Log$
+ * Revision 1.13 2014/03/29 22:04:11 pauloscustodio
+ * Add str_compress_escapes() to compress C-like escape sequences.
+ * Accepts \a, \b, \e, \f, \n, \r, \t, \v, \xhh, \{any} \ooo, allows \0 in the string.
+ *
* Revision 1.12 2014/03/19 23:04:57 pauloscustodio
* Add Str_set_alias() to define an alias char* that always points to self->str


------------------------------------------------------------------------------
Post Reply