From 8289c7bb6506e5307289b6ba4f5d2910dac83560 Mon Sep 17 00:00:00 2001 From: Jakub Kucharski Date: Thu, 18 Feb 2016 09:24:07 +0100 Subject: [PATCH] goo: refactored GooString::resize() & GooString::Set() --- goo/GooString.cc | 53 +++++++++++++++++++++++------------------------------ goo/GooString.h | 10 +++++----- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/goo/GooString.cc b/goo/GooString.cc index 3f22f36..fa106a0 100644 --- a/goo/GooString.cc +++ b/goo/GooString.cc @@ -25,6 +25,7 @@ // Copyright (C) 2012 Pino Toscano // Copyright (C) 2013 Jason Crain // Copyright (C) 2015 William Bader +// Copyright (C) 2016 Jakub Kucharski // // To see a description of the changes please see the Changelog file that // came with your tarball or type make ChangeLog if you are building from git @@ -156,19 +157,19 @@ void inline GooString::resize(int newLength) { else s1 = (char*)grealloc(s, roundedSize(newLength)); } - if (s == sStatic || s1 == sStatic) { + + if (s && (s == sStatic || s1 == sStatic)) { // copy the minimum, we only need to if are moving to or // from sStatic. // assert(s != s1) the roundedSize condition ensures this if (newLength < length) { - memcpy(s1, s, newLength); + memmove(s1, s, newLength); } else { - memcpy(s1, s, length); + memmove(s1, s, length); } if (s != sStatic) gfree(s); } - } s = s1; @@ -176,37 +177,27 @@ void inline GooString::resize(int newLength) { s[length] = '\0'; } -GooString* GooString::Set(const char *s1, int s1Len, const char *s2, int s2Len) +GooString* GooString::Set(const char *newStr, int newLen) { - int newLen = 0; - char *p; - - if (s1) { - if (CALC_STRING_LEN == s1Len) { - s1Len = strlen(s1); - } else - assert(s1Len >= 0); - newLen += s1Len; + if (!newStr) { + if (s != sStatic) { + gfree(s); + } + + s = NULL; + length = 0; + return this; } - if (s2) { - if (CALC_STRING_LEN == s2Len) { - s2Len = strlen(s2); - } else - assert(s2Len >= 0); - newLen += s2Len; + if (newLen == CALC_STRING_LEN) { + newLen = strlen(newStr); + } else { + assert(newLen >= 0); } resize(newLen); - p = s; - if (s1) { - memcpy(p, s1, s1Len); - p += s1Len; - } - if (s2) { - memcpy(p, s2, s2Len); - p += s2Len; - } + memmove(s, newStr, newLen); + return this; } @@ -244,7 +235,9 @@ GooString::GooString(const GooString *str) { GooString::GooString(GooString *str1, GooString *str2) { s = NULL; length = 0; - Set(str1->getCString(), str1->length, str2->getCString(), str2->length); + resize(str1->length + str2->length); + memmove(s, str1->getCString(), str1->length); + memmove(s + str1->length, str2->getCString(), str2->length); } GooString *GooString::fromInt(int x) { diff --git a/goo/GooString.h b/goo/GooString.h index 776dd59..5ff01ef 100644 --- a/goo/GooString.h +++ b/goo/GooString.h @@ -21,6 +21,7 @@ // Copyright (C) 2012-2014 Fabio D'Urso // Copyright (C) 2013 Jason Crain // Copyright (C) 2015 Adam Reichold +// Copyright (C) 2016 Jakub Kucharski // // To see a description of the changes please see the Changelog file that // came with your tarball or type make ChangeLog if you are building from git @@ -65,11 +66,10 @@ public: // Create a string from chars at in . GooString(GooString *str, int idx, int lengthA); - // Set content of a string to concatination of and . They can both - // be NULL. if or is CALC_STRING_LEN, then length of the string - // will be calculated with strlen(). Otherwise we assume they are a valid - // length of string (or its substring) - GooString* Set(const char *s1, int s1Len=CALC_STRING_LEN, const char *s2=NULL, int s2Len=CALC_STRING_LEN); + // Set content of a string to . If is CALC_STRING_LEN, then + // length of the string will be calculated with strlen(). Otherwise we assume + // this is a valid length of (or its substring) + GooString* Set(const char *newStr, int newLen=CALC_STRING_LEN); // Copy a string. explicit GooString(const GooString *str); -- 2.7.1