| 1 | #charset "us-ascii" |
| 2 | #pragma once |
| 3 | |
| 4 | /* |
| 5 | * Copyright 2000, 2006 Michael J. Roberts. |
| 6 | * |
| 7 | * This file is part of TADS 3. |
| 8 | * |
| 9 | * This header defines the StringComparator intrinsic class. |
| 10 | */ |
| 11 | |
| 12 | |
| 13 | /* include our base class definition */ |
| 14 | #include "systype.h" |
| 15 | |
| 16 | /* |
| 17 | * StringComparator intrinsic class. This class provides support for |
| 18 | * dictionaries based on complex string matches, including truncation |
| 19 | * (matching an input word to a dictionary word when the input word is at |
| 20 | * least some minimum length, and matches the dictionary word up to the |
| 21 | * full length of the input word, but the input word is shorter than the |
| 22 | * dictionary word); case folding (matching upper-case letters to |
| 23 | * lower-case letters and vice versa); and character equivalences (for |
| 24 | * matching accented characters to non-accented equivalents, or matching |
| 25 | * special characters to multi-character equivalents, such as matching a |
| 26 | * German "ess-zet" ("sharp-s") ligature to a pair of lower-case "s" |
| 27 | * characters in input). |
| 28 | */ |
| 29 | intrinsic class StringComparator 'string-comparator/030000': Object |
| 30 | { |
| 31 | /* |
| 32 | * Constructor: |
| 33 | * |
| 34 | * new StringComparator(truncLen, caseSensitive, mappings) |
| 35 | * |
| 36 | * truncLen = the minimum truncation length. An input string that |
| 37 | * matches a dictionary string up to the full length of the input |
| 38 | * string, and is shorter than the dictionary string but at least this |
| 39 | * truncation length, will match the dictionary string. If truncLen is |
| 40 | * zero or nil, no truncated matches are allowed. |
| 41 | * |
| 42 | * caseSensitive = true if matches are to be sensitive to case, nil if |
| 43 | * not. If this parameter is nil, then an upper-case letter in an |
| 44 | * input string will match a lower-case letter in a dictionary string, |
| 45 | * and vice versa. If this parameter is true, each character must |
| 46 | * match exactly. |
| 47 | * |
| 48 | * mappings is a list of equivalent character mappings. Each mapping |
| 49 | * in the list is a sublist in this format: |
| 50 | * |
| 51 | *. ['dictChar', 'inputString', ucFlags, lcFlags] |
| 52 | * |
| 53 | * 'dictChar' is a one-character string giving the character to be |
| 54 | * mapped in dictionary strings. 'inputString' is a string of one or |
| 55 | * more characters that is to be considered equivalent to the |
| 56 | * dictionary character when the inputString appears in an input |
| 57 | * string. ucFlags and lcFlags are integer values giving the flag |
| 58 | * values to bitwise-OR into the results when this mapping is used to |
| 59 | * match an upper-case or lower-case input string, respectively. |
| 60 | * |
| 61 | * For example, a mapping to allow the German ess-zet character (whose |
| 62 | * Unicode value is 0x00DF) to match "ss" sequences in input strings, |
| 63 | * with no result flag additions, would look like this: |
| 64 | * |
| 65 | *. ['\u00DF', 'ss', 0, 0] |
| 66 | * |
| 67 | * Only one mapping is allowed for each dictionary character. If more |
| 68 | * than one mapping is given for a single dictionary character, only |
| 69 | * the latest one in the list is actually used. |
| 70 | * |
| 71 | * Flag values 0x0001 through 0x0080 are reserved for use by |
| 72 | * StringComparator itself. Callers are free to use any flag values |
| 73 | * 0x0100 and above. Note that the system flag values are used as |
| 74 | * bitwise OR'd values, so callers should not define any flag values |
| 75 | * 'f' for which (f & 0xFF) != 0. |
| 76 | */ |
| 77 | |
| 78 | /* |
| 79 | * Calculate a hash value. This returns an integer giving the hash |
| 80 | * value for the given string. |
| 81 | */ |
| 82 | calcHash(str); |
| 83 | |
| 84 | /* |
| 85 | * Match two values. The first value is the input string, and the |
| 86 | * second is the dictionary string. Each character in the dictionary |
| 87 | * string can match the corresponding input string character exactly |
| 88 | * (with or without case sensitivity, as specified in our |
| 89 | * constructor), or can match the equivalence mapping sequence for the |
| 90 | * dictionary character. |
| 91 | * |
| 92 | * The return value is zero if the values do not match. If the values |
| 93 | * do match, the return value is a non-zero integer, which will be a |
| 94 | * bitwise OR combination of all of the flag values applicable to the |
| 95 | * match. This is a combination of pre-defined flag values (see |
| 96 | * below) and any flag values from equivalence mappings. The flag |
| 97 | * values from ALL equivalence mappings that were actually used to |
| 98 | * make the match are included. |
| 99 | */ |
| 100 | matchValues(inputStr, dictStr); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Pre-defined matchValues result flags. These are set when applicable in |
| 105 | * the return value of matchValues(). |
| 106 | * |
| 107 | * This class reserves flag values 0x0001 through 0x0080. Callers should |
| 108 | * not use any flag values with any of these bits set. Even though we |
| 109 | * don't define values for all of these flags currently, the ones we don't |
| 110 | * use are reserved for possible use in future versions; to ensure |
| 111 | * compatibility with future versions, callers should not use any of the |
| 112 | * reserved flags for their own purposes. |
| 113 | */ |
| 114 | |
| 115 | /* |
| 116 | * Match - this flag is set in the return code for all matching strings. |
| 117 | * (This flag isn't as useless as it might sound; its purpose is to ensure |
| 118 | * that the return value from matchValues() is non-zero for all matches, |
| 119 | * even when no other flag values are applicable.) |
| 120 | */ |
| 121 | #define StrCompMatch 0x0001 |
| 122 | |
| 123 | /* |
| 124 | * Case folding - this flag is set when the two values match, but one or |
| 125 | * more characters differ in case (in other words, an upper-case letter in |
| 126 | * the input string matched a lower-case letter in the dictionary string, |
| 127 | * or vice versa). |
| 128 | */ |
| 129 | #define StrCompCaseFold 0x0002 |
| 130 | |
| 131 | /* |
| 132 | * Truncation - this flag is set when the input string is shorter than the |
| 133 | * value string (but matches the dictionary completely up to the input |
| 134 | * string's full length, and is at least as long as the truncation length |
| 135 | * specified in the constructor). This flag can only be returned when |
| 136 | * truncation is allowed (as indicated by a non-zero truncation length in |
| 137 | * the constructor), because truncated strings will never match at all |
| 138 | * when truncation isn't allowed. |
| 139 | */ |
| 140 | #define StrCompTrunc 0x0004 |
| 141 |