1 | /* |
---|
2 | * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com> |
---|
3 | * |
---|
4 | * This library is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU Lesser General Public License as |
---|
6 | * published by the Free Software Foundation; either version 2.1 of the |
---|
7 | * License, or (at your option) any later version. This library is |
---|
8 | * distributed in the hope that it will be useful, but WITHOUT ANY |
---|
9 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
10 | * FITNESS FOR A PARTICULAR PURPOSE. |
---|
11 | * See the GNU Lesser General Public License for more details. |
---|
12 | * |
---|
13 | * You should have received a copy of the GNU Lesser General Public License |
---|
14 | * along with this library; if not, write to the Free Software Foundation, |
---|
15 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
16 | */ |
---|
17 | |
---|
18 | /** @file |
---|
19 | * Lexical analysis. |
---|
20 | */ |
---|
21 | |
---|
22 | #include "sys_string.h" |
---|
23 | #include "lexis.h" |
---|
24 | #include <errno.h> |
---|
25 | |
---|
26 | /** Check if a value lies in a (closed) range. |
---|
27 | * |
---|
28 | * @param x value to test |
---|
29 | * @param lo low end of the range |
---|
30 | * @param hi high end of the range |
---|
31 | * @return 1 if x is in the interval [lo, hi], 0 otherwise |
---|
32 | */ |
---|
33 | inline static int in_range(int x, int lo, int hi){ |
---|
34 | return (lo <= x) && (x <= hi); |
---|
35 | } |
---|
36 | |
---|
37 | /** Determine if a string is an (unsigned) decimal number. |
---|
38 | * |
---|
39 | * @param s pointer to characters to test |
---|
40 | * @param n length of string |
---|
41 | * @return 1 if s is a decimal number, 0 otherwise. |
---|
42 | */ |
---|
43 | int is_decimal_number(const char *s, int n){ |
---|
44 | int i; |
---|
45 | if(n <= 0)return 0; |
---|
46 | for(i = 0; i < n; i++){ |
---|
47 | if(!in_decimal_digit_class(s[i])) return 0; |
---|
48 | } |
---|
49 | return 1; |
---|
50 | } |
---|
51 | |
---|
52 | /** Determine if a string is a hex number. |
---|
53 | * Hex numbers are 0, or start with 0x or 0X followed |
---|
54 | * by a non-zero number of hex digits (0-9,a-f,A-F). |
---|
55 | * |
---|
56 | * @param s pointer to characters to test |
---|
57 | * @param n length of string |
---|
58 | * @return 1 if s is a hex number, 0 otherwise. |
---|
59 | */ |
---|
60 | int is_hex_number(const char *s, int n){ |
---|
61 | int i; |
---|
62 | if(n <= 0) return 0; |
---|
63 | if(n == 1){ |
---|
64 | return s[0]=='0'; |
---|
65 | } |
---|
66 | if(n <= 3) return 0; |
---|
67 | if(s[0] != '0' || (s[1] != 'x' && s[1] != 'X')) return 0; |
---|
68 | for(i = 2; i < n; i++){ |
---|
69 | if(!in_hex_digit_class(s[i])) return 0; |
---|
70 | } |
---|
71 | return 1; |
---|
72 | } |
---|
73 | |
---|
74 | /** Test if a string matches a keyword. |
---|
75 | * The comparison is case-insensitive. |
---|
76 | * The comparison fails if either argument is null. |
---|
77 | * |
---|
78 | * @param s string |
---|
79 | * @param k keyword |
---|
80 | * @return 1 if they match, 0 otherwise |
---|
81 | */ |
---|
82 | int is_keyword(const char *s, const char *k){ |
---|
83 | return s && k && !strcasecmp(s, k); |
---|
84 | } |
---|
85 | |
---|
86 | /** Test if a string matches a character. |
---|
87 | * |
---|
88 | * @param s string |
---|
89 | * @param c character (non-null) |
---|
90 | * @return 1 if s contains exactly c, 0 otherwise |
---|
91 | */ |
---|
92 | int is_keychar(const char *s, char c){ |
---|
93 | return c && (s[0] == c) && !s[1]; |
---|
94 | } |
---|