-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtinyatoi.pas
More file actions
45 lines (37 loc) · 777 Bytes
/
dtinyatoi.pas
File metadata and controls
45 lines (37 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
unit dtinyatoi;
// Tiny atoi() replacement. rlyeh, public domain | wtrmrkrlyeh
// Ported to pascal by Doj
{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
interface
function tinyatoi(S: PAnsiChar): PtrInt;
implementation
function tinyatoi(S: PAnsiChar): PtrInt;
var
v, n: PtrInt;
begin
v := 0;
n := 1;
if s <> nil then begin
while s^ = '-' do begin
n := - n;
Inc(s);
end;
while (s^ >= '0') and (s^ <= '9') do begin
v := (10 * v) + (Ord(s^) - Ord('0'));
Inc(s);
end;
end;
Exit(n * v);
end;
//
// begin
// Assert(1230 = tinyatoi('01230'));
// Assert(-1230 = tinyatoi('-01230'));
// Assert(1230 = tinyatoi('--01230'));
// Assert(-1230 = tinyatoi('---01230'));
// end;
//
end.