Skip to content

Commit 1c71117

Browse files
committed
UPDATE:
1. In functon `wrap()`, instead of altering the same object, we will create new string object and return it
1 parent 0f33812 commit 1c71117

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

deps/string/string.cc

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ namespace horizon
334334
return string::str_cmp(this->str, src.str);
335335
}
336336

337-
string &string::wrap(const char *__s)
337+
string string::wrap(const char *__s) const
338338
{
339339
if (__s)
340340
{
@@ -344,14 +344,16 @@ namespace horizon
344344
string::str_catcpy(buffer, __s, b_len);
345345
string::str_catcpy(buffer, this->str, b_len);
346346
string::str_catcpy(buffer, __s, b_len);
347-
this->clear();
348-
this->str = buffer;
349-
this->len = b_len;
347+
348+
string val;
349+
val.str = buffer;
350+
val.len = b_len;
351+
return val;
350352
}
351353
return *this;
352354
}
353355

354-
string &string::wrap(const string &__s)
356+
string string::wrap(const string &__s) const
355357
{
356358
if (__s.str)
357359
{
@@ -361,9 +363,11 @@ namespace horizon
361363
string::str_catcpy(buffer, __s.str, b_len);
362364
string::str_catcpy(buffer, this->str, b_len);
363365
string::str_catcpy(buffer, __s.str, b_len);
364-
this->clear();
365-
this->str = buffer;
366-
this->len = b_len;
366+
367+
string val;
368+
val.str = buffer;
369+
val.len = b_len;
370+
return val;
367371
}
368372
return *this;
369373
}

deps/string/string.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ namespace horizon
6868
[[nodiscard]] bool compare(const char &c) const;
6969
[[nodiscard]] bool compare(const char *src) const;
7070
[[nodiscard]] bool compare(const string &src) const;
71-
string &wrap(const char *__s);
72-
string &wrap(const string &__s);
71+
[[nodiscard]] string wrap(const char *__s) const;
72+
[[nodiscard]] string wrap(const string &__s) const;
7373
string &clear();
7474
string &resize(const std::size_t &new_length);
7575
[[nodiscard]] string substr(const std::size_t &index, std::size_t sub_len = static_cast<std::size_t>(-1)) const;

0 commit comments

Comments
 (0)