aboutsummaryrefslogtreecommitdiff
path: root/src/common/String.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2024-06-01 17:09:44 +0800
committercrupest <crupest@outlook.com>2024-06-08 17:01:55 +0800
commitd1f409530db6f9b712fd672c4c3154cac7eebad1 (patch)
tree1e48a3e2ff14e66aac2503a74df28cca7d85d02c /src/common/String.cpp
parentad796a167e33b54c7fa23ea21c73d57dba4fc928 (diff)
downloadcru-d1f409530db6f9b712fd672c4c3154cac7eebad1.tar.gz
cru-d1f409530db6f9b712fd672c4c3154cac7eebad1.tar.bz2
cru-d1f409530db6f9b712fd672c4c3154cac7eebad1.zip
HALF WORK: refactor something and implement part of subprocess.
Diffstat (limited to 'src/common/String.cpp')
-rw-r--r--src/common/String.cpp33
1 files changed, 30 insertions, 3 deletions
diff --git a/src/common/String.cpp b/src/common/String.cpp
index 0d1c38b8..b2e52e67 100644
--- a/src/common/String.cpp
+++ b/src/common/String.cpp
@@ -1,5 +1,6 @@
#include "cru/common/String.h"
+#include "cru/common/Buffer.h"
#include "cru/common/Exception.h"
#include "cru/common/StringToNumberConverter.h"
#include "cru/common/StringUtil.h"
@@ -31,7 +32,8 @@ String String::FromUtf8(const char* str, Index size) {
String result;
Utf8CodePointIterator iter(str, size);
for (auto cp : iter) {
- Utf16EncodeCodePointAppend(cp, result);
+ Utf16EncodeCodePointAppend(
+ cp, std::bind(&String::push_back, result, std::placeholders::_1));
}
return result;
}
@@ -260,6 +262,10 @@ bool String::EndWith(StringView str) const { return View().EndWith(str); }
std::string String::ToUtf8() const { return View().ToUtf8(); }
+Buffer String::ToUtf8Buffer(bool end_zero) const {
+ return View().ToUtf8Buffer();
+}
+
String& String::TrimStart() {
if (size_ == 0) return *this;
@@ -293,7 +299,9 @@ String& String::Trim() {
}
void String::AppendCodePoint(CodePoint code_point) {
- if (!Utf16EncodeCodePointAppend(code_point, *this)) {
+ if (!Utf16EncodeCodePointAppend(
+ code_point,
+ std::bind(&String::push_back, *this, std::placeholders::_1))) {
throw TextEncodeException(u"Code point out of range.");
}
}
@@ -523,11 +531,30 @@ Range StringView::RangeFromCodePointToCodeUnit(Range code_point_range) const {
std::string StringView::ToUtf8() const {
std::string result;
for (auto cp : CodePointIterator()) {
- Utf8EncodeCodePointAppend(cp, result);
+ Utf8EncodeCodePointAppend(
+ cp, std::bind(&std::string::push_back, result, std::placeholders::_1));
}
return result;
}
+Buffer StringView::ToUtf8Buffer(bool end_zero) const {
+ const Index grow_step = 10;
+ Buffer buffer(grow_step); // Maybe another init value is more reasonable.
+ auto push_back = [&buffer](char c) {
+ if (buffer.IsUsedReachEnd()) {
+ buffer.ResizeBuffer(buffer.GetBufferSize() + grow_step, true);
+ }
+ buffer.PushBack(static_cast<std::byte>(c));
+ };
+ for (auto cp : CodePointIterator()) {
+ Utf8EncodeCodePointAppend(cp, push_back);
+ }
+ if (end_zero) {
+ push_back(0);
+ }
+ return std::move(buffer);
+}
+
int StringView::ParseToInt(Index* processed_characters_count, unsigned flags,
int base) const {
return ParseToInteger<int>(processed_characters_count, flags, base);