]> granicus.if.org Git - re2c/commitdiff
- Constify SubStr
authorhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Thu, 29 Dec 2005 13:08:36 +0000 (13:08 +0000)
committerhelly <helly@642ea486-5414-0410-9d7f-a0204ed87703>
Thu, 29 Dec 2005 13:08:36 +0000 (13:08 +0000)
configure.in
substr.cc
substr.h

index 340ac9518650df099e7b174fb9b7d5b6b9498f48..76840e83a46b563742ee6da9df4fdaa8af980d38 100644 (file)
@@ -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])
index 5fdaeacde4ec9ba47047d82100fff92d24733a80..7ecf777fad25f2b592982d2fae1d818ce03e4b9e 100644 (file)
--- 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;
 }
index 255f6ec62876bd1e29a0ee7e2cca9efcb9426563..a4a43ec27eb195668def6b7d5f3d81dd831db089 100644 (file)
--- 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