]>
git.saurik.com Git - wxWidgets.git/blob - src/regex/regc_locale.c
4 * This file contains locale-specific regexp routines.
5 * This file is #included by regcomp.c.
7 * Copyright (c) 1998 by Scriptics Corporation.
9 * This software is copyrighted by the Regents of the University of
10 * California, Sun Microsystems, Inc., Scriptics Corporation, ActiveState
11 * Corporation and other parties. The following terms apply to all files
12 * associated with the software unless explicitly disclaimed in
15 * The authors hereby grant permission to use, copy, modify, distribute,
16 * and license this software and its documentation for any purpose, provided
17 * that existing copyright notices are retained in all copies and that this
18 * notice is included verbatim in any distributions. No written agreement,
19 * license, or royalty fee is required for any of the authorized uses.
20 * Modifications to this software may be copyrighted by their authors
21 * and need not follow the licensing terms described here, provided that
22 * the new terms are clearly indicated on the first page of each file where
25 * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
26 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
27 * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
28 * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
32 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
33 * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
34 * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
35 * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
38 * GOVERNMENT USE: If you are acquiring this software on behalf of the
39 * U.S. government, the Government shall have only "Restricted Rights"
40 * in the software and related documentation as defined in the Federal
41 * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
42 * are acquiring the software on behalf of the Department of Defense, the
43 * software shall be classified as "Commercial Computer Software" and the
44 * Government shall have only "Restricted Rights" as defined in Clause
45 * 252.227-7013 (c) (1) of DFARs. Notwithstanding the foregoing, the
46 * authors grant the U.S. Government and others acting in its behalf
47 * permission to use and distribute the software in accordance with the
48 * terms specified in this license.
53 int char_and_wchar_strncmp (const char* cp
, const wx_wchar
* wp
, size_t nNum
)
55 while(*cp
++ == (const char)*wp
++ && --nNum
){}
59 int wx_isdigit(wx_wchar c
) {return wxIsdigit(c
);}
60 int wx_isalpha(wx_wchar c
) {return wxIsalpha(c
);}
61 int wx_isalnum(wx_wchar c
) {return wxIsalnum(c
);}
62 int wx_isupper(wx_wchar c
) {return wxIsupper(c
);}
63 int wx_islower(wx_wchar c
) {return wxIslower(c
);}
64 int wx_isgraph(wx_wchar c
) {return wxIsgraph(c
);}
65 int wx_ispunct(wx_wchar c
) {return wxIspunct(c
);}
66 int wx_isspace(wx_wchar c
) {return wxIsspace(c
);}
68 wx_wchar
wx_toupper(wx_wchar c
)
73 wx_wchar
wx_tolower(wx_wchar c
)
78 int wx_strlen(const wx_wchar
* szString
)
81 Generic -- note that some clib functions also test for eol character '^Z'
84 for (; *(szString + nLength) != '\0'; nLength++);
87 return szString
== NULL
? 0 : wxStrlen_(szString
);
89 /* ASCII character-name table */
159 "carriage-return", '\r'
231 "exclamation-mark", '!'
234 "quotation-mark", '"'
252 "left-parenthesis", '('
255 "right-parenthesis", ')'
321 "less-than-sign", '<'
327 "greater-than-sign", '>'
336 "left-square-bracket", '['
342 "reverse-solidus", '\\'
345 "right-square-bracket", ']'
351 "circumflex-accent", '^'
366 "left-curly-bracket", '{'
375 "right-curly-bracket", '}'
390 * nmcces - how many distinct MCCEs are there?
393 nmcces(struct vars
* v
)
396 * No multi-character collating elements defined at the moment.
402 * nleaders - how many chrs can be first chrs of MCCEs?
405 nleaders(struct vars
* v
)
411 * allmcces - return a cvec with all the MCCEs of the locale
414 allmcces(struct vars
* v
, /* context */
415 struct cvec
* cv
) /* this is supposed to have enough room */
417 return clearcvec(cv
);
421 * element - map collating-element name to celt
424 element(struct vars
* v
, /* context */
425 chr
*startp
, /* points to start of name */
426 chr
*endp
) /* points just past end of name */
431 /* generic: one-chr names stand for themselves */
432 assert(startp
< endp
);
440 for (cn
= cnames
; cn
->name
!= NULL
; cn
++)
442 if (strlen(cn
->name
) == len
&&
443 char_and_wchar_strncmp(cn
->name
, startp
, len
) == 0)
445 break; /* NOTE BREAK OUT */
448 if (cn
->name
!= NULL
)
449 return CHR(cn
->code
);
451 /* couldn't find it */
457 * range - supply cvec for a range, including legality check
460 range(struct vars
* v
, /* context */
461 celt a
, /* range start */
462 celt b
, /* range end, might equal a */
463 int cases
) /* case-independent? */
471 if (a
!= b
&& !before(a
, b
))
479 cv
= getcvec(v
, 0, 1, 0);
486 * When case-independent, it's hard to decide when cvec ranges are
487 * usable, so for now at least, we won't try. We allocate enough
488 * space for two case variants plus a little extra for the two title
492 nchrs
= (b
- a
+ 1) * 2 + 4;
494 cv
= getcvec(v
, nchrs
, 0, 0);
497 for (c
= a
; c
<= b
; c
++)
500 lc
= wx_tolower((chr
) c
);
503 uc
= wx_toupper((chr
) c
);
512 * before - is celt x before celt y, for purposes of range legality?
514 static int /* predicate */
515 before(celt x
, celt y
)
517 /* trivial because no MCCEs */
524 * eclass - supply cvec for an equivalence class
525 * Must include case counterparts on request.
528 eclass(struct vars
* v
, /* context */
529 celt c
, /* Collating element representing the
530 * equivalence class. */
531 int cases
) /* all cases? */
535 /* crude fake equivalence class for testing */
536 if ((v
->cflags
& REG_FAKE
) && c
== 'x')
538 cv
= getcvec(v
, 4, 0, 0);
539 addchr(cv
, (chr
) 'x');
540 addchr(cv
, (chr
) 'y');
543 addchr(cv
, (chr
) 'X');
544 addchr(cv
, (chr
) 'Y');
549 /* otherwise, none */
551 return allcases(v
, c
);
552 cv
= getcvec(v
, 1, 0, 0);
559 * cclass - supply cvec for a character class
561 * Must include case counterparts on request.
564 cclass(struct vars
* v
, /* context */
565 chr
*startp
, /* where the name starts */
566 chr
*endp
, /* just past the end of the name */
567 int cases
) /* case-independent? */
570 struct cvec
*cv
= NULL
;
576 * The following arrays define the valid character class names.
579 static char *classNames
[] = {
580 "alnum", "alpha", "ascii", "blank", "cntrl", "digit", "graph",
581 "lower", "print", "punct", "space", "upper", "xdigit", NULL
586 CC_ALNUM
, CC_ALPHA
, CC_ASCII
, CC_BLANK
, CC_CNTRL
, CC_DIGIT
, CC_GRAPH
,
587 CC_LOWER
, CC_PRINT
, CC_PUNCT
, CC_SPACE
, CC_UPPER
, CC_XDIGIT
591 * Map the name to the corresponding enumerated value.
595 for (namePtr
= classNames
, i
= 0; *namePtr
!= NULL
; namePtr
++, i
++)
597 if (strlen(*namePtr
) == len
&&
598 char_and_wchar_strncmp(*namePtr
, startp
, len
) == 0)
611 * Remap lower and upper to alpha if the match is case insensitive.
615 ((enum classes
) index
== CC_LOWER
||
616 (enum classes
) index
== CC_UPPER
))
617 index
= (int) CC_ALPHA
;
620 * Now compute the character class contents.
622 * For the moment, assume that only char codes < 256 can be in these
626 switch ((enum classes
) index
)
630 cv
= getcvec(v
, UCHAR_MAX
, 1, 0);
633 for (i
= 0; i
<= UCHAR_MAX
; i
++)
635 if (wx_isalpha((chr
) i
))
638 addrange(cv
, (chr
) '0', (chr
) '9');
642 cv
= getcvec(v
, UCHAR_MAX
, 0, 0);
645 for (i
= 0; i
<= UCHAR_MAX
; i
++)
647 if (wx_isalpha((chr
) i
))
653 cv
= getcvec(v
, 0, 1, 0);
655 addrange(cv
, 0, 0x7f);
658 cv
= getcvec(v
, 2, 0, 0);
663 cv
= getcvec(v
, 0, 2, 0);
664 addrange(cv
, 0x0, 0x1f);
665 addrange(cv
, 0x7f, 0x9f);
668 cv
= getcvec(v
, 0, 1, 0);
670 addrange(cv
, (chr
) '0', (chr
) '9');
673 cv
= getcvec(v
, UCHAR_MAX
, 0, 0);
676 for (i
= 0; i
<= UCHAR_MAX
; i
++)
678 if (wx_ispunct((chr
) i
))
684 cv
= getcvec(v
, 0, 3, 0);
687 addrange(cv
, '0', '9');
688 addrange(cv
, 'a', 'f');
689 addrange(cv
, 'A', 'F');
693 cv
= getcvec(v
, UCHAR_MAX
, 0, 0);
696 for (i
= 0; i
<= UCHAR_MAX
; i
++)
698 if (wx_isspace((chr
) i
))
704 cv
= getcvec(v
, UCHAR_MAX
, 0, 0);
707 for (i
= 0; i
<= UCHAR_MAX
; i
++)
709 if (wx_islower((chr
) i
))
715 cv
= getcvec(v
, UCHAR_MAX
, 0, 0);
718 for (i
= 0; i
<= UCHAR_MAX
; i
++)
720 if (wx_isupper((chr
) i
))
726 cv
= getcvec(v
, UCHAR_MAX
, 0, 0);
729 for (i
= 0; i
<= UCHAR_MAX
; i
++)
731 if (wx_isgraph((chr
) i
))
743 * allcases - supply cvec for all case counterparts of a chr (including itself)
745 * This is a shortcut, preferably an efficient one, for simple characters;
746 * messy cases are done via range().
749 allcases(struct vars
* v
, /* context */
750 chr pc
) /* character to get case equivs of */
757 lc
= wx_tolower((chr
) c
);
758 uc
= wx_toupper((chr
) c
);
760 cv
= getcvec(v
, 2, 0, 0);
768 * cmp - chr-substring compare
770 * Backrefs need this. It should preferably be efficient.
771 * Note that it does not need to report anything except equal/unequal.
772 * Note also that the length is exact, and the comparison should not
773 * stop at embedded NULs!
775 static int /* 0 for equal, nonzero for unequal */
776 cmp(const chr
*x
, const chr
*y
, /* strings to compare */
777 size_t len
) /* exact length of comparison */
779 return memcmp(VS(x
), VS(y
), len
* sizeof(chr
));
783 * casecmp - case-independent chr-substring compare
785 * REG_ICASE backrefs need this. It should preferably be efficient.
786 * Note that it does not need to report anything except equal/unequal.
787 * Note also that the length is exact, and the comparison should not
788 * stop at embedded NULs!
790 static int /* 0 for equal, nonzero for unequal */
791 casecmp(const chr
*x
, const chr
*y
, /* strings to compare */
792 size_t len
) /* exact length of comparison */
794 for (; len
> 0; len
--, x
++, y
++)
796 if ((*x
!= *y
) && (wx_tolower(*x
) != wx_tolower(*y
)))