-
Notifications
You must be signed in to change notification settings - Fork 12
Add UTF-16 Strings #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
6c99515
dd57179
1d53e8f
9039c3c
c2047f9
27175ef
98b3fc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module; | ||
| export module core.containers; | ||
| export import core.containers.string; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,101 @@ | ||||||
| module; | ||||||
|
|
||||||
| #include <cstring> | ||||||
|
|
||||||
| module core.containers.string; | ||||||
|
|
||||||
| namespace draco::containers { | ||||||
|
|
||||||
| StringBuilder::Error StringBuilder::Reserve(isize newCapacity) { | ||||||
| if (newCapacity <= capacity) { | ||||||
| return Error::Okay; | ||||||
| } | ||||||
|
|
||||||
| Slice newDst; | ||||||
| StringBuilder::Error err = (StringBuilder::Error)allocator.alloc(&newDst, newCapacity * sizeof(utf16), alignof(utf16)); | ||||||
| if (err != Error::Okay) { | ||||||
| return err; | ||||||
| } | ||||||
|
|
||||||
| memcpy(newDst.data, buffer, size * sizeof(utf16)); | ||||||
|
|
||||||
| Slice oldDst = { | ||||||
| .data = buffer, | ||||||
| .size = (usize)capacity * sizeof(utf16) | ||||||
| }; | ||||||
| allocator.free(oldDst); | ||||||
|
|
||||||
| capacity = newDst.size / sizeof(utf16); | ||||||
| buffer = (utf16 *)newDst.data; | ||||||
|
|
||||||
| return Error::Okay; | ||||||
| } | ||||||
|
|
||||||
| StringBuilder::Error StringBuilder::GrowCapacity(isize minCapacity) { | ||||||
| if (capacity >= minCapacity) { | ||||||
| return Error::Okay; | ||||||
| } | ||||||
|
|
||||||
| isize newCapacity = capacity + draco::math::max(capacity / 2, minCapacity); | ||||||
| return Reserve(newCapacity); | ||||||
| } | ||||||
|
|
||||||
| StringBuilder::Error StringBuilder::Write(utf16 c) { | ||||||
| Error err = GrowCapacity(size + 1); | ||||||
| if (err != Error::Okay) { | ||||||
| return err; | ||||||
| } | ||||||
|
|
||||||
| buffer[size++] = c; | ||||||
| return Error::Okay; | ||||||
| } | ||||||
|
|
||||||
| StringBuilder::Error StringBuilder::Write(rune r) { | ||||||
| if (r > 0x10FFFF || (r >= 0xD800 && r <= 0xDFFF)) { | ||||||
| return Error::InvalidRune; | ||||||
| } | ||||||
|
|
||||||
| Error err = GrowCapacity(size + 2); | ||||||
| if (err != Error::Okay) { | ||||||
| return err; | ||||||
| } | ||||||
|
|
||||||
| if (r <= 0xFFFF) { | ||||||
| buffer[size++] = (utf16)r; | ||||||
| return Error::Okay; | ||||||
| } | ||||||
|
|
||||||
| r -= 0x10000; | ||||||
|
|
||||||
| utf16 high = 0xD800 + (r >> 10); | ||||||
| utf16 low = 0xDC00 + (r & 0x3FF); | ||||||
|
|
||||||
| buffer[size++] = high; | ||||||
| buffer[size++] = low; | ||||||
|
|
||||||
| return Error::Okay; | ||||||
| } | ||||||
|
|
||||||
| StringBuilder::Error StringBuilder::Write(utf16 const *str) { | ||||||
| // TODO(Jon) assert not null | ||||||
| isize length = StringLength(str); | ||||||
| Error err = GrowCapacity(size + length); | ||||||
|
Comment on lines
+79
to
+82
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing null handling in At Line 81, 🤖 Prompt for AI Agents |
||||||
| if (err != Error::Okay) { | ||||||
| return err; | ||||||
| } | ||||||
| memcpy(buffer + size, str, length * sizeof(utf16)); | ||||||
| size += length; | ||||||
| return Error::Okay; | ||||||
| } | ||||||
|
|
||||||
| StringBuilder::Error StringBuilder::Write(String str) { | ||||||
| Error err = GrowCapacity(size + str.size); | ||||||
| if (err != Error::Okay) { | ||||||
| return err; | ||||||
| } | ||||||
| memcpy(buffer + size, str.text, str.size * sizeof(utf16)); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use overlap-safe copy when appending from a At Line 96, Suggested fix- memcpy(buffer + size, str.text, str.size * sizeof(utf16));
+ memmove(buffer + size, str.text, str.size * sizeof(utf16));📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| size += str.size; | ||||||
| return Error::Okay; | ||||||
| } | ||||||
|
|
||||||
| } //namespace draco::containers | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for future issue: Make slice version of memcpy.