]>
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
){}
60 /* ASCII character-name table */
130 "carriage-return", '\r'
202 "exclamation-mark", '!'
205 "quotation-mark", '"'
223 "left-parenthesis", '('
226 "right-parenthesis", ')'
292 "less-than-sign", '<'
298 "greater-than-sign", '>'
307 "left-square-bracket", '['
313 "reverse-solidus", '\\'
316 "right-square-bracket", ']'
322 "circumflex-accent", '^'
337 "left-curly-bracket", '{'
346 "right-curly-bracket", '}'
361 * nmcces - how many distinct MCCEs are there?
364 nmcces(struct vars
* v
)
367 * No multi-character collating elements defined at the moment.
373 * nleaders - how many chrs can be first chrs of MCCEs?
376 nleaders(struct vars
* v
)
382 * allmcces - return a cvec with all the MCCEs of the locale
385 allmcces(struct vars
* v
, /* context */
386 struct cvec
* cv
) /* this is supposed to have enough room */
388 return clearcvec(cv
);
392 * element - map collating-element name to celt
395 element(struct vars
* v
, /* context */
396 chr
*startp
, /* points to start of name */
397 chr
*endp
) /* points just past end of name */
402 /* generic: one-chr names stand for themselves */
403 assert(startp
< endp
);
411 for (cn
= cnames
; cn
->name
!= NULL
; cn
++)
413 if (strlen(cn
->name
) == len
&&
414 char_and_wchar_strncmp(cn
->name
, startp
, len
) == 0)
416 break; /* NOTE BREAK OUT */
419 if (cn
->name
!= NULL
)
420 return CHR(cn
->code
);
422 /* couldn't find it */
428 * range - supply cvec for a range, including legality check
431 range(struct vars
* v
, /* context */
432 celt a
, /* range start */
433 celt b
, /* range end, might equal a */
434 int cases
) /* case-independent? */
442 if (a
!= b
&& !before(a
, b
))
450 cv
= getcvec(v
, 0, 1, 0);
457 * When case-independent, it's hard to decide when cvec ranges are
458 * usable, so for now at least, we won't try. We allocate enough
459 * space for two case variants plus a little extra for the two title
463 nchrs
= (b
- a
+ 1) * 2 + 4;
465 cv
= getcvec(v
, nchrs
, 0, 0);
468 for (c
= a
; c
<= b
; c
++)
471 lc
= wx_tolower((chr
) c
);
474 uc
= wx_toupper((chr
) c
);
483 * before - is celt x before celt y, for purposes of range legality?
485 static int /* predicate */
486 before(celt x
, celt y
)
488 /* trivial because no MCCEs */
495 * eclass - supply cvec for an equivalence class
496 * Must include case counterparts on request.
499 eclass(struct vars
* v
, /* context */
500 celt c
, /* Collating element representing the
501 * equivalence class. */
502 int cases
) /* all cases? */
506 /* crude fake equivalence class for testing */
507 if ((v
->cflags
& REG_FAKE
) && c
== 'x')
509 cv
= getcvec(v
, 4, 0, 0);
510 addchr(cv
, (chr
) 'x');
511 addchr(cv
, (chr
) 'y');
514 addchr(cv
, (chr
) 'X');
515 addchr(cv
, (chr
) 'Y');
520 /* otherwise, none */
522 return allcases(v
, c
);
523 cv
= getcvec(v
, 1, 0, 0);
530 * cclass - supply cvec for a character class
532 * Must include case counterparts on request.
535 cclass(struct vars
* v
, /* context */
536 chr
*startp
, /* where the name starts */
537 chr
*endp
, /* just past the end of the name */
538 int cases
) /* case-independent? */
541 struct cvec
*cv
= NULL
;
547 * The following arrays define the valid character class names.
550 static char *classNames
[] = {
551 "alnum", "alpha", "ascii", "blank", "cntrl", "digit", "graph",
552 "lower", "print", "punct", "space", "upper", "xdigit", NULL
557 CC_ALNUM
, CC_ALPHA
, CC_ASCII
, CC_BLANK
, CC_CNTRL
, CC_DIGIT
, CC_GRAPH
,
558 CC_LOWER
, CC_PRINT
, CC_PUNCT
, CC_SPACE
, CC_UPPER
, CC_XDIGIT
562 * Map the name to the corresponding enumerated value.
566 for (namePtr
= classNames
, i
= 0; *namePtr
!= NULL
; namePtr
++, i
++)
568 if (strlen(*namePtr
) == len
&&
569 char_and_wchar_strncmp(*namePtr
, startp
, len
) == 0)
582 * Remap lower and upper to alpha if the match is case insensitive.
586 ((enum classes
) index
== CC_LOWER
||
587 (enum classes
) index
== CC_UPPER
))
588 index
= (int) CC_ALPHA
;
591 * Now compute the character class contents.
593 * For the moment, assume that only char codes < 256 can be in these
597 switch ((enum classes
) index
)
601 cv
= getcvec(v
, UCHAR_MAX
, 1, 0);
604 for (i
= 0; i
<= UCHAR_MAX
; i
++)
606 if (wx_isalpha((chr
) i
))
609 addrange(cv
, (chr
) '0', (chr
) '9');
613 cv
= getcvec(v
, UCHAR_MAX
, 0, 0);
616 for (i
= 0; i
<= UCHAR_MAX
; i
++)
618 if (wx_isalpha((chr
) i
))
624 cv
= getcvec(v
, 0, 1, 0);
626 addrange(cv
, 0, 0x7f);
629 cv
= getcvec(v
, 2, 0, 0);
634 cv
= getcvec(v
, 0, 2, 0);
635 addrange(cv
, 0x0, 0x1f);
636 addrange(cv
, 0x7f, 0x9f);
639 cv
= getcvec(v
, 0, 1, 0);
641 addrange(cv
, (chr
) '0', (chr
) '9');
644 cv
= getcvec(v
, UCHAR_MAX
, 0, 0);
647 for (i
= 0; i
<= UCHAR_MAX
; i
++)
649 if (wx_ispunct((chr
) i
))
655 cv
= getcvec(v
, 0, 3, 0);
658 addrange(cv
, '0', '9');
659 addrange(cv
, 'a', 'f');
660 addrange(cv
, 'A', 'F');
664 cv
= getcvec(v
, UCHAR_MAX
, 0, 0);
667 for (i
= 0; i
<= UCHAR_MAX
; i
++)
669 if (wx_isspace((chr
) i
))
675 cv
= getcvec(v
, UCHAR_MAX
, 0, 0);
678 for (i
= 0; i
<= UCHAR_MAX
; i
++)
680 if (wx_islower((chr
) i
))
686 cv
= getcvec(v
, UCHAR_MAX
, 0, 0);
689 for (i
= 0; i
<= UCHAR_MAX
; i
++)
691 if (wx_isupper((chr
) i
))
697 cv
= getcvec(v
, UCHAR_MAX
, 0, 0);
700 for (i
= 0; i
<= UCHAR_MAX
; i
++)
702 if (wx_isgraph((chr
) i
))
714 * allcases - supply cvec for all case counterparts of a chr (including itself)
716 * This is a shortcut, preferably an efficient one, for simple characters;
717 * messy cases are done via range().
720 allcases(struct vars
* v
, /* context */
721 chr pc
) /* character to get case equivs of */
728 lc
= wx_tolower((chr
) c
);
729 uc
= wx_toupper((chr
) c
);
731 cv
= getcvec(v
, 2, 0, 0);
739 * cmp - chr-substring compare
741 * Backrefs need this. It should preferably be efficient.
742 * Note that it does not need to report anything except equal/unequal.
743 * Note also that the length is exact, and the comparison should not
744 * stop at embedded NULs!
746 static int /* 0 for equal, nonzero for unequal */
747 cmp(const chr
*x
, const chr
*y
, /* strings to compare */
748 size_t len
) /* exact length of comparison */
750 return memcmp(VS(x
), VS(y
), len
* sizeof(chr
));
754 * casecmp - case-independent chr-substring compare
756 * REG_ICASE backrefs need this. It should preferably be efficient.
757 * Note that it does not need to report anything except equal/unequal.
758 * Note also that the length is exact, and the comparison should not
759 * stop at embedded NULs!
761 static int /* 0 for equal, nonzero for unequal */
762 casecmp(const chr
*x
, const chr
*y
, /* strings to compare */
763 size_t len
) /* exact length of comparison */
765 for (; len
> 0; len
--, x
++, y
++)
767 if ((*x
!= *y
) && (wx_tolower(*x
) != wx_tolower(*y
)))