summaryrefslogtreecommitdiff
path: root/Libraries/LibC/wchar.cpp
diff options
context:
space:
mode:
authorEmanuel Sprung <emanuel.sprung@gmail.com>2019-11-11 14:08:20 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-11-11 22:04:16 +0100
commite7affa24dca571911694e262b18f6de00f882c35 (patch)
tree5c38867e3cd9b69f231a21ee1eaefa378d14e03c /Libraries/LibC/wchar.cpp
parent3042c942d871b40a3d7dbb06079d29babefb780d (diff)
downloadserenity-e7affa24dca571911694e262b18f6de00f882c35.zip
LibC, LibM: Add functions needed to compile python3
Diffstat (limited to 'Libraries/LibC/wchar.cpp')
-rw-r--r--Libraries/LibC/wchar.cpp84
1 files changed, 84 insertions, 0 deletions
diff --git a/Libraries/LibC/wchar.cpp b/Libraries/LibC/wchar.cpp
index e1bc54a7c5..4308c6745b 100644
--- a/Libraries/LibC/wchar.cpp
+++ b/Libraries/LibC/wchar.cpp
@@ -1,3 +1,4 @@
+#include <AK/Assertions.h>
#include <wchar.h>
extern "C" {
@@ -18,6 +19,14 @@ wchar_t* wcscpy(wchar_t* dest, const wchar_t* src)
return originalDest;
}
+wchar_t* wcsncpy(wchar_t* dest, const wchar_t* src, size_t num)
+{
+ wchar_t* originalDest = dest;
+ while (((*dest++ = *src++) != '\0') && ((size_t)(dest - originalDest) < num))
+ ;
+ return originalDest;
+}
+
int wcscmp(const wchar_t* s1, const wchar_t* s2)
{
while (*s1 == *s2++)
@@ -37,4 +46,79 @@ wchar_t* wcschr(const wchar_t* str, int c)
}
}
+const wchar_t* wcsrchr(const wchar_t* str, wchar_t wc)
+{
+ wchar_t* last = nullptr;
+ wchar_t c;
+ for (; (c = *str); ++str) {
+ if (c == wc)
+ last = const_cast<wchar_t*>(str);
+ }
+ return last;
+}
+
+wchar_t* wcscat(wchar_t* dest, const wchar_t* src)
+{
+ size_t dest_length = wcslen(dest);
+ size_t i;
+ for (i = 0; src[i] != '\0'; i++)
+ dest[dest_length + i] = src[i];
+ dest[dest_length + i] = '\0';
+ return dest;
+}
+
+wchar_t* wcstok(wchar_t* str, const wchar_t* delim, wchar_t** ptr)
+{
+ wchar_t* used_str = str;
+ if (!used_str) {
+ used_str = *ptr;
+ }
+
+ size_t token_start = 0;
+ size_t token_end = 0;
+ size_t str_len = wcslen(used_str);
+ size_t delim_len = wcslen(delim);
+
+ for (size_t i = 0; i < str_len; ++i) {
+ bool is_proper_delim = false;
+
+ for (size_t j = 0; j < delim_len; ++j) {
+ if (used_str[i] == delim[j]) {
+ // Skip beginning delimiters
+ if (token_end - token_start == 0) {
+ ++token_start;
+ break;
+ }
+
+ is_proper_delim = true;
+ }
+ }
+
+ ++token_end;
+ if (is_proper_delim && token_end > 0) {
+ --token_end;
+ break;
+ }
+ }
+
+ if (used_str[token_start] == '\0')
+ return nullptr;
+
+ if (token_end == 0) {
+ return &used_str[token_start];
+ }
+
+ used_str[token_end] = '\0';
+ return &used_str[token_start];
+}
+
+wchar_t* wcsncat(wchar_t* dest, const wchar_t* src, size_t n)
+{
+ size_t dest_length = wcslen(dest);
+ size_t i;
+ for (i = 0; i < n && src[i] != '\0'; i++)
+ dest[dest_length + i] = src[i];
+ dest[dest_length + i] = '\0';
+ return dest;
+}
}