ia64/xen-unstable

view tools/xfrd/lexis.c @ 2422:2274a0386cc9

bitkeeper revision 1.1159.69.5 (4138e882jA1YaR_OfTfNHe_uT4PDIg)

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