diff options
Diffstat (limited to 'src/libs')
| -rw-r--r-- | src/libs/math.cc | 12 | ||||
| -rw-r--r-- | src/libs/math.hpp | 10 | ||||
| -rw-r--r-- | src/libs/stdio.cc | 6 | ||||
| -rw-r--r-- | src/libs/stdio.hpp | 2 | ||||
| -rw-r--r-- | src/libs/string.cc | 36 | ||||
| -rw-r--r-- | src/libs/string.hpp | 9 |
6 files changed, 44 insertions, 31 deletions
diff --git a/src/libs/math.cc b/src/libs/math.cc index 63487b2..27f009a 100644 --- a/src/libs/math.cc +++ b/src/libs/math.cc @@ -1,31 +1,31 @@ #include "math.hpp" -int pow(int x, int n) { +u32 pow(u32 x, u32 n) { if (n < 0) return -1; else if (n == 0) return 1; else if (n == 1) return x; - int ret = x; - for (int i = 0; i < (n - 1); i++) + u32 ret = x; + for (u32 i = 0; i < (n - 1); i++) ret *= x; return ret; } -int max(int x, int y) { +u32 max(u32 x, u32 y) { if (x > y) return x; return y; } -int min(int x, int y) { +u32 min(u32 x, u32 y) { if (x < y) return x; return y; } -int abs(int x) { +u32 abs(u32 x) { if (x < 0) return -x; return x; diff --git a/src/libs/math.hpp b/src/libs/math.hpp index 1eaac1c..c5b7c46 100644 --- a/src/libs/math.hpp +++ b/src/libs/math.hpp @@ -1,6 +1,8 @@ #pragma once -int pow(int x, int n); -int max(int x, int y); -int min(int x, int y); -int abs(int x); +#include "core/types.hpp" + +u32 pow(u32 x, u32 n); +u32 max(u32 x, u32 y); +u32 min(u32 x, u32 y); +u32 abs(u32 x); diff --git a/src/libs/stdio.cc b/src/libs/stdio.cc index 4a20c05..e73e4af 100644 --- a/src/libs/stdio.cc +++ b/src/libs/stdio.cc @@ -109,12 +109,12 @@ void printh(int h) { itoh(h, str); print(str); } -void printh(int h, int size) { +void printh(int h, u32 size) { char str[17]; char str2[17]; itoh(h, str); - int a = 0; - for (int i = min(max(16 - size, 0), 15); i < 16; i++) { + u32 a = 0; + for (u32 i = min(max(16 - size, 0), 15); i < 16; i++) { str2[a] = str[i]; a++; } diff --git a/src/libs/stdio.hpp b/src/libs/stdio.hpp index f48ae81..fd266a5 100644 --- a/src/libs/stdio.hpp +++ b/src/libs/stdio.hpp @@ -33,4 +33,4 @@ void printh(int h); /** * Print an integer as hex using itoh() truncated to size */ -void printh(int h, int size); +void printh(int h, u32 size); diff --git a/src/libs/string.cc b/src/libs/string.cc index f8ae3ca..73c3318 100644 --- a/src/libs/string.cc +++ b/src/libs/string.cc @@ -1,16 +1,16 @@ #include "string.hpp" #include "math.hpp" -void memcpy(void* src, void* dst, int size){ - char *c_src=(char*)src; - char *c_dst=(char*)dst; - for(int i=0;i<size;i++) - *c_dst=*c_src; +void memcpy(void* src, void* dst, u32 size){ + u8 *c_src=(u8*)src; + u8 *c_dst=(u8*)dst; + for(u32 i=0;i<size;i++) + *(c_dst+i)=*(c_src+i); } void itoa(u64 i, char *a){ // Check if lower than 0 - char neg=0; + u8 neg=0; if(i<0){ neg=1; i=-i; @@ -18,17 +18,17 @@ void itoa(u64 i, char *a){ } // Count number of digits - int len=1; + u32 len=1; while(i/pow(10,len)>=1) { len++; } // Build string - int max_pow=len-1; - for(int j=0;j<=max_pow;j++){ - int cur_pow=pow(10,max_pow-j); - char digit=i/cur_pow; + u32 max_pow=len-1; + for(u32 j=0;j<=max_pow;j++){ + u32 cur_pow=pow(10,max_pow-j); + u8 digit=i/cur_pow; a[j+neg]='0'+digit; i=i-digit*cur_pow; // Remove first digits (most significant) } @@ -45,7 +45,7 @@ void itoh(u64 i, char *a){ u32 i_a=i&0xFFFFFFFF; u32 i_b=i>>32; - for(char j=0;j<8;j++){ + for(u8 j=0;j<8;j++){ u64 t=(j*4); u64 mask=0xF; mask=mask << t; @@ -53,7 +53,7 @@ void itoh(u64 i, char *a){ a[15-j]=hex[index]; } - for(char j=0;j<8;j++){ + for(u8 j=0;j<8;j++){ u64 t=(j*4); u64 mask=0xF; mask=mask << t; @@ -63,9 +63,15 @@ void itoh(u64 i, char *a){ a[16]='\0'; } -int strlen(char *s){ - int i=0; +u32 strlen(char *s){ + u32 i=0; while(s[i]!='\0') i++; return i; +} + +void substr(u32 s, u32 e, char *src, char *dst){ + u32 size=abs(e-s)+1; + memcpy(src+s, dst, size); + dst[size]='\0'; }
\ No newline at end of file diff --git a/src/libs/string.hpp b/src/libs/string.hpp index 305b9a8..a7dce0a 100644 --- a/src/libs/string.hpp +++ b/src/libs/string.hpp @@ -5,7 +5,7 @@ /** * Copy data byte per byte from src to dst */ -void memcpy(void *src, void *dst, int size); +void memcpy(void *src, void *dst, u32 size); /** * Convert int to char array @@ -20,4 +20,9 @@ void itoh(u64 i, char *a); /** * Length of a char* */ -int strlen(char *s); +u32 strlen(char *s); + +/** + * Substr + */ +void substr(u32 s, u32 e, char *src, char *dst); |
