1. Edit procedure check exception 12 (overflow stack)
[kolibrios.git] / programs / string.inc
blobd954b87698bf56dad36d347e5cee2dd53ca66911
1  proc string.length uses ebx, _str\r
2     mov     eax, 0\r
3     mov     ebx, [_str]\r
4   @@:\r
5     cmp     [ebx], byte 0\r
6     je      @f\r
7     inc     eax\r
8     inc     ebx\r
9     jmp     @b\r
10   @@:\r
11     ret\r
12  endp\r
14  proc string.copy uses eax ebx ecx, _src, _dst\r
15     mov     eax, [_src]\r
16     mov     ebx, [_dst]\r
17   @@:\r
18     mov     cl, [eax]\r
19     mov     [ebx], cl\r
20     cmp     cl, 0\r
21     je      @f\r
22     inc     eax\r
23     inc     ebx\r
24     jmp     @b\r
25   @@:\r
26     ret\r
27  endp\r
29  proc string.concatenate uses eax, _src, _dst\r
30     stdcall string.length, [_dst]\r
31     add     eax, [_dst]\r
32     stdcall string.copy, [_src], eax\r
33     ret\r
34  endp\r
36  proc string.cmp uses ecx esi edi, _str1, _str2, _n\r
37     mov     ecx, [_n]\r
38     test    ecx, ecx         ; Max length is zero?\r
39     je      .done\r
41     mov     esi, [_str1]        ; esi = string s1\r
42     mov     edi, [_str2]        ; edi = string s2\r
43     cld\r
44  .compare:\r
45     cmpsb                    ; Compare two bytes\r
46     jne     .done\r
47     cmp     byte [esi-1], 0  ; End of string?\r
48     je      .done\r
49     dec     ecx              ; Length limit reached?\r
50     jne     .compare\r
51  .done:\r
52     seta    al               ; al = (s1 > s2)\r
53     setb    ah               ; ah = (s1 < s2)\r
54     sub     al, ah\r
55     movsx   eax, al          ; eax = (s1 > s2) - (s1 < s2), i.e. -1, 0, 1\r
56     ret\r
57  endp\r
59  proc string.to_lower_case uses eax, _str\r
60     mov     eax, [_str]\r
61   @@:\r
62     cmp     [eax], byte 0\r
63     je      @f\r
64     cmp     [eax], byte 65\r
65     jl      .next\r
66     cmp     [eax], byte 90\r
67     jg      .next\r
68     add     [eax], byte 97 - 65\r
69  .next:\r
70     inc     eax\r
71     jmp     @b\r
72   @@:\r
73     ret\r
74  endp\r
76  proc string.to_upper_case uses eax, _str\r
77     mov     eax, [_str]\r
78   @@:\r
79     cmp     [eax], byte 0\r
80     je      @f\r
81     cmp     [eax], byte 97\r
82     jl      .next\r
83     cmp     [eax], byte 122\r
84     jg      .next\r
85     sub     [eax], byte 97 - 65\r
86  .next:\r
87     inc     eax\r
88     jmp     @b\r
89   @@:\r
90     ret\r
91  endp\r
93  proc string.match uses ebx ecx edx, _str1, _str2\r
94     mov     ebx, [_str1]\r
95     mov     ecx, [_str2]\r
96   @@:\r
97     cmp     [ebx], byte 0\r
98     je      @f\r
99     cmp     [ecx], byte 0\r
100     je      @f\r
102     mov     dl, [ebx]\r
103     cmp     [ecx], byte '?'\r
104     je      .next\r
105     cmp     [ecx], byte '*'\r
106     je      .next_ebx\r
107     cmp     [ecx], dl\r
108     je      .next\r
110     cmp     [ecx - 1], byte '*'\r
111     je      .next_ecx\r
113     jmp     @f\r
115  .next_ecx:\r
116     dec     ecx\r
117     jmp     .next\r
118  .next_ebx:\r
119     dec     ebx\r
120  .next:\r
121     inc     ebx\r
122     inc     ecx\r
123     jmp     @b\r
124   @@:\r
126   @@:\r
127     cmp     [ecx], byte 0\r
128     je      @f\r
129     cmp     [ecx], byte '*'\r
130     jne     @f\r
131     inc     ecx\r
132     jmp     @b\r
133   @@:\r
135     cmp     [ecx], byte 0\r
136     je      @f\r
137     mov     eax, 0\r
138     ret\r
139   @@:\r
140     mov     eax, 1\r
141     ret\r
142  endp\r
144  proc string.trim_last uses eax, _str\r
145     stdcall string.length, [_str]\r
146     add     eax, [_str]\r
147     dec     eax\r
148   @@:\r
149     cmp     [eax], byte ' '\r
150     jne     @f\r
151     mov     [eax], byte 0\r
152     dec     eax\r
153     jmp     @b\r
154   @@:\r
155     ret\r
156  endp\r
158  proc string.trim_first, _str\r
159     mov     eax, [_str]\r
160   @@:\r
161     cmp     [eax], byte ' '\r
162     jne     @f\r
163     inc     eax\r
164     jmp     @b\r
165   @@:\r
166     ret\r
167  endp\r
169  proc string.index_of uses ebx ecx, _str, _char, _num\r
170     mov     ebx, [_char]\r
171     mov     ecx, [_str]\r
172     mov     eax, 0\r
173   @@:\r
174     cmp     [ecx], byte 0\r
175     je      @f\r
176     cmp     [ecx], bl\r
177     jne     .after_check\r
178     dec     [_num]\r
179     jz      .finded\r
180  .after_check:\r
181     inc     ecx\r
182     inc     eax\r
183     jmp     @b\r
184   @@:\r
185     mov     eax, -1\r
186  .finded:\r
187     ret\r
188  endp\r
190  proc string.last_index_of uses ebx ecx, _str, _char, _num\r
191     stdcall string.length, [_str]\r
192     mov     ecx, [_str]\r
193     add     ecx, eax\r
194     mov     ebx, [_char]\r
195   @@:\r
196     cmp     eax, 0\r
197     je      @f\r
198     cmp     [ecx], bl\r
199     jne     .after_check\r
200     dec     [_num]\r
201     jz      .finded\r
202  .after_check:\r
203     dec     ecx\r
204     dec     eax\r
205     jmp     @b\r
206   @@:\r
207     mov     eax, -2\r
208  .finded:\r
209     inc     eax\r
210     ret\r
211  endp