OSDN Git Service

klibc基本機能実装. ACPICAの準備
[vaneos/DivergeMirror.git] / lib / klibc / string / str.c
1 /*
2         str.c
3         C Standard Library string.h
4         str*
5         (C) 2014-2015 VOSystems.
6 */
7
8 #include<Diverge/types.h>
9
10 int strcmp(const char *s1, const char *s2)
11 {
12         while(*s1==*s2) {
13                 if(*s1=='\0')return 0;
14                 s1++;
15                 s2++;
16         }
17         return ((unsigned char)*s1 - (unsigned char)*s2);
18 }
19
20 size_t strlen(const char *s)
21 {
22         size_t len=0;
23
24         while(*s++)len++;
25         return len;
26 }
27
28 char *strcpy(char *s1, const char *s2)
29 {
30         char *p=s1;
31         while(*s1++ =*s2++);
32         return p;
33 }
34
35 char *strchr(const char *s, int c)
36 {
37         c=(char)c;
38         while(*s!=c){
39                 if (*s=='\0')return NULL;
40                 s++;
41         }
42         return (char *)s;
43 }
44
45 char *strstr(const char *s1, const char *s2)
46 {
47         if(*s2=='\0')return (char *)s1;
48
49         for(; (s1=strchr(s1, *s2))!=NULL; ++s1){
50                 const char *sc1=s1;
51                 const char *sc2=s2;
52
53                 do{
54                         if(*++sc2=='\0')return (char *)s1;
55                 }while(*++sc1==*sc2);
56         }
57
58         return NULL;
59 }
60
61 size_t strcspn(const char *s1, const char *s2)
62 {
63         const char *p=s1;
64
65         for(; *s1; s1++){
66                 const char *t;
67
68                 for(t=s2; *t; t++)
69                         if(*t==*s1)return s1-p;
70         }
71         return s1-p;
72 }
73
74 char *strpbrk(const char *s1, const char *s2)
75 {
76         for(; *s1; s1++){
77                 const char *t=s2;
78                 for(; *t; t++)
79                         if(*t==*s1)return (char *)s1;
80         }
81         return NULL;
82 }
83
84 char *strcat(char *s1, const char *s2)
85 {
86         char  *p = s1;
87
88         while (*s1) s1++;
89         while (*s1++ = *s2++);
90         return p;
91 }
92
93 int _space_sign(const char *s, const char **endptr)
94 {
95         while (isspace((unsigned char)*s))++s;
96         int sign = 0;
97         switch (*s)
98         {
99                 case '-':
100                         sign = -1;
101                         // fall through
102                 case '+':
103                         ++s;
104                         break;
105         }
106         *endptr = s;
107         return sign;
108 }
109
110 unsigned long strtoul(const char* s, char** endptr, int radix)
111 {
112         unsigned long result;
113
114         if (_space_sign(s, (const char**)&s) != 0)--s;  // '-'の位置まで戻す
115
116         if (s[0] == '0'){
117                 ++s;
118                 if ((s[1] | 0x20) == 'x'){
119                         if (radix == 0 || radix == 16){
120                                 ++s;
121                                 radix = 16;
122                         }
123                 }else if (radix == 0){
124                         radix = 8;
125                 }
126         }else if (radix == 0){
127                 radix = 10;
128         }
129         
130         int c;
131         for (result = 0; c = tolower((unsigned char)*s), isdigit(c) || ('a' <= c && c <= 'z'); s++){
132                 int d = isdigit(c) ? c - '0' : c - 'a' + 10;
133                 if (d >= radix)break;
134                 if (result > (ULONG_MAX - d) / radix){
135                         result = ULONG_MAX;
136                 }else{
137                         result = result * radix + d;
138                 }
139         }
140
141         if (endptr != NULL)*endptr = (char*)s;
142
143         return result;
144 }