From 05224a9fd9e2a6140b25cad34ab6c7da697cb014 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 8 Apr 2002 00:30:39 +0000 Subject: [PATCH] Make dual functions: _new (which allocates) and _init (which just copies) to make C conversion elsewhere easier. svn path=/trunk/yasm/; revision=580 --- tools/re2c/substr.c | 17 ++++++++++++++++- tools/re2c/substr.h | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/tools/re2c/substr.c b/tools/re2c/substr.c index 26529bc4..de18f0d1 100644 --- a/tools/re2c/substr.c +++ b/tools/re2c/substr.c @@ -13,6 +13,13 @@ SubStr_eq(const SubStr *s1, const SubStr *s2) return (s1->len == s2->len && memcmp(s1->str, s2->str, s1->len) == 0); } +void +Str_init(Str *r, const SubStr* s) +{ + SubStr_init(r, malloc(sizeof(char)*s->len), s->len); + memcpy(r->str, s->str, s->len); +} + Str * Str_new(const SubStr* s) { @@ -21,8 +28,16 @@ Str_new(const SubStr* s) return r; } +void +Str_copy(Str *r, Str* s) +{ + SubStr_init(r, s->str, s->len); + s->str = NULL; + s->len = 0; +} + Str * -Str_copy(Str* s) +Str_new_copy(Str* s) { Str *r = SubStr_new(s->str, s->len); s->str = NULL; diff --git a/tools/re2c/substr.h b/tools/re2c/substr.h index c1db6fa5..9ee849d7 100644 --- a/tools/re2c/substr.h +++ b/tools/re2c/substr.h @@ -13,19 +13,37 @@ struct SubStr { typedef struct SubStr SubStr; int SubStr_eq(const SubStr *, const SubStr *); + +static inline void SubStr_init_u(SubStr*, uchar*, uint); static inline SubStr *SubStr_new_u(uchar*, uint); + +static inline void SubStr_init(SubStr*, char*, uint); static inline SubStr *SubStr_new(char*, uint); -static inline SubStr *SubStr_copy(const SubStr*); -void SubStr_out(const SubStr *, FILE *); + +static inline void SubStr_copy(SubStr*, const SubStr*); +static inline SubStr *SubStr_new_copy(const SubStr*); + +void SubStr_out(const SubStr*, FILE *); #define SubStr_delete(x) free(x) typedef struct SubStr Str; +void Str_init(Str*, const SubStr*); Str *Str_new(const SubStr*); -Str *Str_copy(Str*); + +void Str_copy(Str*, Str*); +Str *Str_new_copy(Str*); + Str *Str_new_empty(void); void Str_delete(Str *); +static inline void +SubStr_init_u(SubStr *r, uchar *s, uint l) +{ + r->str = (char*)s; + r->len = l; +} + static inline SubStr * SubStr_new_u(uchar *s, uint l) { @@ -35,6 +53,13 @@ SubStr_new_u(uchar *s, uint l) return r; } +static inline void +SubStr_init(SubStr *r, char *s, uint l) +{ + r->str = s; + r->len = l; +} + static inline SubStr * SubStr_new(char *s, uint l) { @@ -44,8 +69,15 @@ SubStr_new(char *s, uint l) return r; } +static inline void +SubStr_copy(SubStr *r, const SubStr *s) +{ + r->str = s->str; + r->len = s->len; +} + static inline SubStr * -SubStr_copy(const SubStr *s) +SubStr_new_copy(const SubStr *s) { SubStr *r = malloc(sizeof(SubStr)); r->str = s->str;