aboutsummaryrefslogtreecommitdiff
path: root/kernel/lib/strings.c
diff options
context:
space:
mode:
authorChristian Cunningham <cc@localhost>2022-03-24 09:38:08 -0700
committerChristian Cunningham <cc@localhost>2022-03-24 09:38:08 -0700
commit93bf62580a68533dc8252b9a2a055c02f34ecb67 (patch)
tree1b1ca92ebbe107a998136a1442c0dba5be885e13 /kernel/lib/strings.c
parent3e64dda5d5c350cc325650133f7e64967f1efe84 (diff)
Modularized
Diffstat (limited to 'kernel/lib/strings.c')
-rw-r--r--kernel/lib/strings.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/kernel/lib/strings.c b/kernel/lib/strings.c
new file mode 100644
index 0000000..674af19
--- /dev/null
+++ b/kernel/lib/strings.c
@@ -0,0 +1,119 @@
+#include <lib/kmem.h>
+#include <lib/strings.h>
+
+unsigned long strlen(string_t s)
+{
+ unsigned long len = 0;
+ while (s[len] != 0) {
+ len += 1;
+ }
+ return len;
+}
+
+void strcpy(string_t src, string_t dest)
+{
+ unsigned long idx = 0;
+ while (src[idx] != 0) {
+ dest[idx] = src[idx];
+ idx++;
+ }
+ dest[idx] = src[idx];
+}
+
+unsigned char strcmp(string_t a, string_t b)
+{
+ unsigned long idx = 0;
+ while (a[idx] != 0 && b[idx] != 0) {
+ if (a[idx] != b[idx]) {
+ return 0;
+ }
+ idx += 1;
+ }
+ return a[idx] == b[idx];
+}
+
+unsigned char strcmpn(string_t a, string_t b, unsigned int n)
+{
+ unsigned long idx = 0;
+ while (a[idx] != 0 && b[idx] != 0 && idx+1 < n) {
+ if (a[idx] != b[idx]) {
+ return 0;
+ }
+ idx += 1;
+ }
+ return a[idx] == b[idx];
+}
+
+char* zhex32_to_str(unsigned long value)
+{
+ static char data[10];
+ char tmp = 0;
+ char isz = -1;
+ for (int i = 0; i < 8; i++) {
+ tmp = (value >> 4*(8-i-1))&0xF;
+ if (isz == 0xFF && tmp != 0)
+ isz = i;
+ if(tmp > 0x9)
+ tmp += 7;
+ tmp += 0x30;
+ data[i] = tmp;
+ }
+ return data+isz;
+}
+
+char* hex32_to_str(unsigned long value)
+{
+ static char data[10];
+ char tmp = 0;
+ for (int i = 0; i < 8; i++) {
+ tmp = (value >> 4*(8-i-1))&0xF;
+ if(tmp > 0x9)
+ tmp += 7;
+ tmp += 0x30;
+ data[i] = tmp;
+ }
+ return data;
+}
+
+char* u32_to_str(unsigned long value)
+{
+ unsigned long t = value;
+ unsigned long c;
+ static char data[12];
+ char* dptr = data + 9;
+ for (int i = 0; i <= 10; i++) {
+ c = t%10;
+ *dptr = 0x30 + (c&0xF);
+ t /= 10;
+ if (t==0)
+ break;
+ dptr -= 1;
+ }
+ return dptr;
+}
+
+char* s32_to_str(unsigned long value)
+{
+ long t = value;
+ unsigned long c;
+ char is_neg = 0;
+ if (t < 0) {
+ t = -t;
+ is_neg = 1;
+ }
+ static char data[13];
+ char* dptr = data + 10;
+ for (int i = 0; i <= 10; i++) {
+ c = t%10;
+ *dptr = 0x30 + (c&0xF);
+ t /= 10;
+ if (t==0)
+ break;
+ dptr -= 1;
+ }
+ if (is_neg) {
+ dptr -= 1;
+ *dptr = '-';
+ }
+ return dptr;
+}