-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.plm
113 lines (104 loc) · 2.63 KB
/
string.plm
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/****h* plm/string
*
* NAME
* String
*
* DESCRIPTION
* Subroutines to manipulate null-terminated string
*
******
*/
/****f* string/string_equal
*
* NAME
* string_equal -- tell if two strings are equal
*
* SYNOPSIS
* string_equal(p$str1, p$str2)
*
* DESCRIPTION
* Compares two null-terminated strings and tells if they are equal are not.
* Both arguments p$str1 and p$str2 are pointers to the first character
* of the respective string. Any of these strings can be empty, and
* should be null-terminated.
*
* RETURN VALUE
* Returns the boolean value true if strings are equal and false otherwise.
*
******
*/
string_equal: procedure (p$str1, p$str2) boolean public;
declare (p$str1, p$str2) pointer;
declare str1 based p$str1 (*) byte;
declare str2 based p$str2 (*) byte;
declare i integer;
i = 0;
do while str1(i) <> 0 and str2(i) <> 0 and str1(i) = str2(i);
i = i + 1;
end;
return str1(i) = 0 and str2(i) = 0;
end string_equal;
/****f* string/char_upper
*
* NAME
* char_upper -- convert a character to uppercase
*
* SYNOPSIS
* char_upper(c)
*
* DESCRIPTION
* This function converts one character to uppercase.
*
******
*/
char_upper: procedure (c) byte public;
declare c byte;
if c >= 'a' and c <= 'z' then
return c and 05fh;
else
return c;
end char_upper;
parse$hexchar: procedure (b) integer;
declare b byte;
declare result integer;
if b >= 'A' and b <= 'F' then
result = int(b - 'A' + 10);
else if b >= 'a' and 'b' <= 'f' then
result = int(b - 'a' + 10);
else if b >= '0' and b <= '9' then
result = int(b - '0');
else
result = -1;
return result;
end parse$hexchar;
/****f* string/parse_hex
*
* NAME
* parse_hex -- convert hexadecimal string to number
*
* SYNOPSIS
* parse_hex(p$str)
*
* DESCRIPTION
* Convert a string representation of a hexadecimal number into an unsigned
* 16-bit integer. This function reads characters pointed by p$str and
* stops when a character other than 0-9, a-f, A-F is encountered.
* This function is case insensitive.
*
******
*/
parse_hex: procedure (p$str) uint public;
declare p$str pointer;
declare str based p$str (*) byte;
declare i uint;
declare result uint;
declare n integer;
i = 0;
result = 0;
do while (n := parse$hexchar(str(i))) <> -1;
result = result * 16 + unsign(n);
i = i + 1;
end;
return result;
end parse_hex;