]> granicus.if.org Git - flex/commitdiff
Added utility functions to deal with character case.
authorJohn Millaway <john43@users.sourceforge.net>
Mon, 16 Dec 2002 23:28:22 +0000 (23:28 +0000)
committerJohn Millaway <john43@users.sourceforge.net>
Mon, 16 Dec 2002 23:28:22 +0000 (23:28 +0000)
ccl.c
flexdef.h

diff --git a/ccl.c b/ccl.c
index e53f7bac1f238c78c21fb0deb4679c63bacb2121..1e3c0bbbe3da4fc59554f164597bf6a09371bfdd 100644 (file)
--- a/ccl.c
+++ b/ccl.c
@@ -158,3 +158,42 @@ void    list_character_set (file, cset)
 
        putc (']', file);
 }
+
+/** Determines if the range [c1-c2] is unambiguous in a case-insensitive
+ * scanner.  Specifically, if a lowercase or uppercase character, x, is in the
+ * range [c1-c2], then we require that UPPERCASE(x) and LOWERCASE(x) must also
+ * be in the range. If not, then this range is ambiguous, and the function
+ * returns false.  For example, [@-_] spans [a-z] but not [A-Z].  Beware that
+ * [a-z] will be labeled ambiguous because it does not include [A-Z].
+ *
+ * @param c1 the lower end of the range
+ * @param c2 the upper end of the range
+ * @return true if [c1-c2] is not ambiguous for a caseless scanner.
+ */
+bool range_covers_case (int c1, int c2)
+{
+       int     i, o;
+
+       for (i = c1; i <= c2; i++) {
+               if (has_case (i)) {
+                       o = reverse_case (i);
+                       if (o < c1 || c2 < o)
+                               return false;
+               }
+       }
+       return true;
+}
+
+/** Reverse the case of a character, if possible.
+ * @return c if case-reversal does not apply.
+ */
+int reverse_case (int c)
+{
+       return isupper (c) ? tolower (c) : (islower (c) ? toupper (c) : c);
+}
+
+/** Return true if c is uppercase or lowercase. */
+bool has_case (int c)
+{
+       return (isupper (c) || islower (c)) ? true : false;
+}
index 17a7e23878d818ec686caf208da13918968166bb..e83422ba26044cfda2e3aa4571b2da250426bda5 100644 (file)
--- a/flexdef.h
+++ b/flexdef.h
@@ -1105,5 +1105,28 @@ extern jmp_buf flex_main_jmp_buf;
 /* Removes all \n and \r chars from tail of str. returns str. */
 extern char *chomp (char *str);
 
+/* ctype functions forced to return boolean */
+#define b_isalnum(c) (isalnum(c)?true:false)
+#define b_isalpha(c) (isalpha(c)?true:false)
+#define b_isascii(c) (isascii(c)?true:false)
+#define b_isblank(c) (isblank(c)?true:false)
+#define b_iscntrl(c) (iscntrl(c)?true:false)
+#define b_isdigit(c) (isdigit(c)?true:false)
+#define b_isgraph(c) (isgraph(c)?true:false)
+#define b_islower(c) (islower(c)?true:false)
+#define b_isprint(c) (isprint(c)?true:false)
+#define b_ispunct(c) (ispunct(c)?true:false)
+#define b_isspace(c) (isspace(c)?true:false)
+#define b_isupper(c) (isupper(c)?true:false)
+#define b_isxdigit(c) (isxdigit(c)?true:false)
+
+/* return true if char is uppercase or lowercase. */
+bool has_case(int c);
+
+/* Change case of character if possible. */
+int reverse_case(int c);
+
+/* return false if [c1-c2] is ambiguous for a caseless scanner. */
+bool range_covers_case (int c1, int c2);
 
 #endif /* not defined FLEXDEF_H */