From bdea976e50c540f6d8d93ef643965be7c6abdb43 Mon Sep 17 00:00:00 2001 From: Jakub Kucharski Date: Tue, 10 May 2016 10:03:43 +0200 Subject: [PATCH] goo: refactored GooString::Set() --- goo/GooString.cc | 40 ++++++++++++++-------------------------- goo/GooString.h | 10 +++++----- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/goo/GooString.cc b/goo/GooString.cc index 3f22f36..de9c93c 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 @@ -176,37 +177,22 @@ 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) { + clear(); + 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 +230,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); + memcpy(s, str1->getCString(), str1->length); + memcpy(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.8.2