From: helly Date: Thu, 29 Dec 2005 13:08:36 +0000 (+0000) Subject: - Constify SubStr X-Git-Tag: 0.13.6~559 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ad7345393421b1c5f7e659f0b43db4a974785933;p=re2c - Constify SubStr --- diff --git a/configure.in b/configure.in index 340ac951..76840e83 100644 --- a/configure.in +++ b/configure.in @@ -34,7 +34,7 @@ AC_C_VOLATILE AC_FUNC_MALLOC AC_FUNC_MEMCMP AC_FUNC_MMAP -AC_CHECK_FUNCS([memset munmap strdup]) +AC_CHECK_FUNCS([memset munmap strdup strndup]) AC_CHECK_SIZEOF([char]) AC_CHECK_SIZEOF([short]) diff --git a/substr.cc b/substr.cc index 5fdaeacd..7ecf777f 100644 --- a/substr.cc +++ b/substr.cc @@ -3,6 +3,19 @@ #include "substr.h" #include "globals.h" +#ifndef HAVE_STRNDUP + +char *strndup(const char *str, size_t len) +{ + char * ret = (char*)malloc(len + 1); + + memcpy(ret, str, len); + ret[len] = '\0'; + return ret; +} + +#endif + namespace re2c { @@ -22,9 +35,9 @@ bool operator==(const SubStr &s1, const SubStr &s2) return (bool) (s1.len == s2.len && memcmp(s1.str, s2.str, s1.len) == 0); } -Str::Str(const SubStr& s) : SubStr(new char[s.len], s.len) +Str::Str(const SubStr& s) : SubStr(strndup(s.str, s.len), s.len) { - memcpy(str, s.str, s.len); + ; } Str::Str(Str& s) : SubStr(s.str, s.len) @@ -41,7 +54,7 @@ Str::Str() : SubStr((char*) NULL, 0) Str::~Str() { - delete str; + free((void*)str); str = (char*) - 1; len = (uint) - 1; } diff --git a/substr.h b/substr.h index 255f6ec6..a4a43ec2 100644 --- a/substr.h +++ b/substr.h @@ -13,14 +13,14 @@ class SubStr { public: - char *str; - uint len; + const char * str; + uint len; public: friend bool operator==(const SubStr &, const SubStr &); - SubStr(uchar*, uint); - SubStr(char*, uint); - SubStr(char*); + SubStr(const uchar*, uint); + SubStr(const char*, uint); + SubStr(const char*); SubStr(const SubStr&); void out(std::ostream&) const; std::string to_string() const @@ -50,15 +50,15 @@ inline std::ostream& operator<<(std::ostream& o, const SubStr* s) return o << *s; } -inline SubStr::SubStr(uchar *s, uint l) +inline SubStr::SubStr(const uchar *s, uint l) : str((char*) s), len(l) { } -inline SubStr::SubStr(char *s, uint l) +inline SubStr::SubStr(const char *s, uint l) : str(s), len(l) { } -inline SubStr::SubStr(char *s) +inline SubStr::SubStr(const char *s) : str(s), len(strlen(s)) { } @@ -68,4 +68,10 @@ inline SubStr::SubStr(const SubStr &s) } // end namespace re2c +#ifndef HAVE_STRNDUP + +char *strndup(const char *str, size_t len); + +#endif + #endif