| 1 |
|
| /* ------------------------------------------------------------------------ |
| 2 |
|
| @NAME : string_util.c |
| 3 |
|
| @DESCRIPTION: Various string-processing utility functions: |
| 4 |
|
| bt_purify_string() |
| 5 |
|
| bt_change_case() |
| 6 |
|
| |
| 7 |
|
| Both follow BibTeX's rules (purify$ and change.case$), |
| 8 |
|
| including its treatment of "special characters" -- a brace |
| 9 |
|
| group at depth 0 that starts with a backslash, eg. {\'e}. |
| 10 |
|
| TeX control sequences are only ever ASCII letters; ordinary |
| 11 |
|
| text may be any Unicode, and is classified and case-mapped |
| 12 |
|
| character by character (never byte by byte). Both functions |
| 13 |
|
| return a new string, since Unicode case mapping can change |
| 14 |
|
| the length of a string. |
| 15 |
|
| @CREATED : 1997/10/19, Greg Ward |
| 16 |
|
| -------------------------------------------------------------------------- */ |
| 17 |
|
| |
| 18 |
|
| #include <stdlib.h> |
| 19 |
|
| #include <string.h> |
| 20 |
|
| #include "bt_internal.h" |
| 21 |
|
| |
| 22 |
|
| /* |
| 23 |
|
| * LaTeX's "foreign letter" control sequences, and their upper- and |
| 24 |
|
| * lowercase forms (Kopka and Daly, *A Guide to LaTeX 2e*, section 2.5.6). |
| 25 |
|
| */ |
| 26 |
|
| typedef enum |
| 27 |
|
| { |
| 28 |
|
| L_OTHER, /* not a "foreign" letter */ |
| 29 |
|
| L_OSLASH_L, /* Eastern European {\o} */ |
| 30 |
|
| L_OSLASH_U, |
| 31 |
|
| L_LSLASH_L, /* {\l} */ |
| 32 |
|
| L_LSLASH_U, |
| 33 |
|
| L_OELIG_L, /* Latin {\oe} ligature */ |
| 34 |
|
| L_OELIG_U, |
| 35 |
|
| L_AELIG_L, /* {\ae} ligature */ |
| 36 |
|
| L_AELIG_U, |
| 37 |
|
| L_SSHARP_L, /* German "sharp s" {\ss} */ |
| 38 |
|
| L_SSHARP_U, |
| 39 |
|
| L_ACIRCLE_L, /* Nordic {\aa} */ |
| 40 |
|
| L_ACIRCLE_U, |
| 41 |
|
| L_INODOT_L, /* undotted i: {\i} */ |
| 42 |
|
| L_JNODOT_L /* {\j} */ |
| 43 |
|
| } bt_letter; |
| 44 |
|
| |
| 45 |
|
| static const char * uc_version[] = |
| 46 |
|
| { |
| 47 |
|
| NULL, "\\O", "\\O", "\\L", "\\L", "\\OE", "\\OE", "\\AE", "\\AE", |
| 48 |
|
| "SS", /* L_SSHARP_L -- for LaTeX 2.09 */ |
| 49 |
|
| "\\SS", "\\AA", "\\AA", "I", "J" |
| 50 |
|
| }; |
| 51 |
|
| |
| 52 |
|
| static const char * lc_version[] = |
| 53 |
|
| { |
| 54 |
|
| NULL, "\\o", "\\o", "\\l", "\\l", "\\oe", "\\oe", "\\ae", "\\ae", |
| 55 |
|
| "\\ss", "\\ss", "\\aa", "\\aa", "\\i", "\\j" |
| 56 |
|
| }; |
| 57 |
|
| |
| 58 |
|
| /* Is str[start..stop) one of the foreign letter control sequences? */ |
| 59 |
|
| static boolean |
| 60 |
53 |
| foreign_letter (const char *str, int start, int stop, bt_letter *letter) |
| 61 |
|
| { |
| 62 |
53 |
| char c1 = str[start]; |
| 63 |
53 |
▶ | char c2 = (stop - start > 1) ? str[start + 1] : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| char c2 = (stop - start > 1) ? str[start + 1] : 0; |
18 |
35 |
53 |
|
| 64 |
|
| |
| 65 |
53 |
| *letter = L_OTHER; |
| 66 |
53 |
| switch (stop - start) |
| 67 |
|
| { |
| 68 |
34 |
| case 1: |
| 69 |
34 |
| switch (c1) |
| 70 |
|
| { |
| 71 |
1 |
| case 'o': *letter = L_OSLASH_L; break; |
| 72 |
0 |
| case 'O': *letter = L_OSLASH_U; break; |
| 73 |
1 |
| case 'l': *letter = L_LSLASH_L; break; |
| 74 |
0 |
| case 'L': *letter = L_LSLASH_L; break; |
| 75 |
0 |
| case 'i': *letter = L_INODOT_L; break; |
| 76 |
0 |
| case 'j': *letter = L_JNODOT_L; break; |
| 77 |
|
| } |
| 78 |
34 |
| break; |
| 79 |
15 |
| case 2: |
| 80 |
15 |
▶ | if (c1 == 'o' && c2 == 'e') *letter = L_OELIG_L; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c1 == 'o' && c2 == 'e') *letter = L_OELIG_L; |
2 |
13 |
15 |
| if (c1 == 'o' && c2 == 'e') *letter = L_OELIG_L; |
2 |
0 |
2 |
|
| 81 |
13 |
▶ | else if (c1 == 'O' && c2 == 'E') *letter = L_OELIG_U; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c1 == 'O' && c2 == 'E') *letter = L_OELIG_U; |
0 |
13 |
13 |
| else if (c1 == 'O' && c2 == 'E') *letter = L_OELIG_U; |
0 |
0 |
0 |
|
| 82 |
13 |
▶ | else if (c1 == 'a' && c2 == 'e') *letter = L_AELIG_L; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c1 == 'a' && c2 == 'e') *letter = L_AELIG_L; |
3 |
10 |
13 |
| else if (c1 == 'a' && c2 == 'e') *letter = L_AELIG_L; |
2 |
1 |
3 |
|
| 83 |
11 |
▶ | else if (c1 == 'A' && c2 == 'E') *letter = L_AELIG_U; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c1 == 'A' && c2 == 'E') *letter = L_AELIG_U; |
1 |
10 |
11 |
| else if (c1 == 'A' && c2 == 'E') *letter = L_AELIG_U; |
0 |
1 |
1 |
|
| 84 |
11 |
▶ | else if (c1 == 'a' && c2 == 'a') *letter = L_ACIRCLE_L; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c1 == 'a' && c2 == 'a') *letter = L_ACIRCLE_L; |
1 |
10 |
11 |
| else if (c1 == 'a' && c2 == 'a') *letter = L_ACIRCLE_L; |
1 |
0 |
1 |
|
| 85 |
10 |
▶ | else if (c1 == 'A' && c2 == 'A') *letter = L_ACIRCLE_U; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c1 == 'A' && c2 == 'A') *letter = L_ACIRCLE_U; |
1 |
9 |
10 |
| else if (c1 == 'A' && c2 == 'A') *letter = L_ACIRCLE_U; |
1 |
0 |
1 |
|
| 86 |
9 |
▶ | else if (c1 == 's' && c2 == 's') *letter = L_SSHARP_L; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c1 == 's' && c2 == 's') *letter = L_SSHARP_L; |
9 |
0 |
9 |
| else if (c1 == 's' && c2 == 's') *letter = L_SSHARP_L; |
9 |
0 |
9 |
|
| 87 |
0 |
▶ | else if (c1 == 'S' && c2 == 'S') *letter = L_SSHARP_U; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (c1 == 'S' && c2 == 'S') *letter = L_SSHARP_U; |
0 |
0 |
0 |
| else if (c1 == 'S' && c2 == 'S') *letter = L_SSHARP_U; |
0 |
0 |
0 |
|
| 88 |
15 |
| break; |
| 89 |
|
| } |
| 90 |
53 |
| return *letter != L_OTHER; |
| 91 |
|
| } |
| 92 |
|
| |
| 93 |
|
| /* Length of the character at s, or 1 at end of string */ |
| 94 |
|
| static size_t |
| 95 |
663 |
| charlen (const char *s) |
| 96 |
|
| { |
| 97 |
663 |
| size_t n = bt_utf8_charlen (s); |
| 98 |
663 |
▶ | return n ? n : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| return n ? n : 1; |
663 |
0 |
663 |
|
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
|
| |
| 102 |
|
| /* ------------------------------------------------------------------------ |
| 103 |
|
| @NAME : purify_special_char() |
| 104 |
|
| @DESCRIPTION: "Purifies" a BibTeX special character. On input, *src |
| 105 |
|
| points to the opening brace of a special character. Skips |
| 106 |
|
| the opening brace and backslash; if the control sequence is |
| 107 |
|
| a foreign letter it is copied, otherwise it is skipped. Text |
| 108 |
|
| after the control sequence is copied if it consists of |
| 109 |
|
| letters (and combining marks), and skipped otherwise. On |
| 110 |
|
| return *src points to the closing brace. |
| 111 |
|
| -------------------------------------------------------------------------- */ |
| 112 |
|
| static void |
| 113 |
50 |
| purify_special_char (const char *str, size_t *src, bt_sbuf *out) |
| 114 |
|
| { |
| 115 |
50 |
| int depth = 1; |
| 116 |
|
| size_t peek; |
| 117 |
|
| bt_letter letter; |
| 118 |
|
| |
| 119 |
50 |
| *src += 2; /* jump to start of control sequence */ |
| 120 |
50 |
| peek = *src; /* scan to end of control sequence */ |
| 121 |
96 |
▶ | while (BT_IS_ALPHA (str[peek])) |
Branch: 4 branches
| Branch | True | False | Total |
| while (BT_IS_ALPHA (str[peek])) |
55 |
41 |
96 |
| while (BT_IS_ALPHA (str[peek])) |
42 |
13 |
55 |
| while (BT_IS_ALPHA (str[peek])) |
20 |
34 |
54 |
| while (BT_IS_ALPHA (str[peek])) |
4 |
16 |
20 |
|
| 122 |
46 |
| peek++; |
| 123 |
50 |
▶ | if (peek == *src && str[peek]) /* single-char, non-alpha control */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (peek == *src && str[peek]) /* single-char, non-alpha control */ |
26 |
24 |
50 |
| if (peek == *src && str[peek]) /* single-char, non-alpha control */ |
26 |
0 |
26 |
|
| 124 |
26 |
| peek += charlen (str + peek); /* sequence (eg. {\'e}) */ |
| 125 |
|
| |
| 126 |
50 |
▶ | if (foreign_letter (str, (int) *src, (int) peek, &letter)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (foreign_letter (str, (int) *src, (int) peek, &letter)) |
15 |
35 |
50 |
|
| 127 |
|
| { |
| 128 |
15 |
| bt_sbuf_putc (out, str[(*src)++]); /* copy first char */ |
| 129 |
15 |
▶ | if (*src < peek) /* and second, */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (*src < peek) /* and second, */ |
13 |
2 |
15 |
|
| 130 |
|
| { /* downcased */ |
| 131 |
13 |
| char c = str[(*src)++]; |
| 132 |
13 |
▶ | bt_sbuf_putc (out, BT_TOLOWER (c)); |
Branch: 2 branches
| Branch | True | False | Total |
| bt_sbuf_putc (out, BT_TOLOWER (c)); |
13 |
0 |
13 |
| bt_sbuf_putc (out, BT_TOLOWER (c)); |
1 |
12 |
13 |
|
| 133 |
|
| } |
| 134 |
|
| } |
| 135 |
|
| else |
| 136 |
35 |
| *src = peek; /* skip the control sequence */ |
| 137 |
|
| |
| 138 |
164 |
▶ | while (str[*src]) |
Branch: 1 branch
| Branch | True | False | Total |
| while (str[*src]) |
164 |
0 |
164 |
|
| 139 |
|
| { |
| 140 |
164 |
| switch (str[*src]) |
| 141 |
|
| { |
| 142 |
16 |
| case '{': |
| 143 |
16 |
| depth++; |
| 144 |
16 |
| (*src)++; |
| 145 |
16 |
| break; |
| 146 |
66 |
| case '}': |
| 147 |
66 |
| depth--; |
| 148 |
66 |
▶ | if (depth == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth == 0) |
50 |
16 |
66 |
|
| 149 |
50 |
| return; /* done with special char */ |
| 150 |
16 |
| (*src)++; |
| 151 |
16 |
| break; |
| 152 |
82 |
| default: |
| 153 |
|
| { |
| 154 |
82 |
| size_t n = charlen (str + *src); |
| 155 |
82 |
▶ | if (bt_uni_isalpha (str + *src) || bt_uni_ismark (str + *src)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (bt_uni_isalpha (str + *src) || bt_uni_ismark (str + *src)) |
32 |
50 |
82 |
| if (bt_uni_isalpha (str + *src) || bt_uni_ismark (str + *src)) |
0 |
32 |
32 |
|
| 156 |
50 |
| bt_sbuf_putn (out, str + *src, n); |
| 157 |
82 |
| *src += n; |
| 158 |
|
| } |
| 159 |
|
| } |
| 160 |
|
| } |
| 161 |
|
| /* Unbalanced braces: no warning, as BibTeX doesn't warn here either |
| 162 |
|
| * (and the lexer will already have complained). */ |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
|
| |
| 166 |
|
| /* ------------------------------------------------------------------------ |
| 167 |
|
| @NAME : bt_purify_string() |
| 168 |
|
| @DESCRIPTION: "Purifies" a BibTeX string: alphanumeric characters (and |
| 169 |
|
| combining marks) are copied, hyphens and ties become spaces, |
| 170 |
|
| spaces are copied, and everything else is skipped. Special |
| 171 |
|
| characters are handled by purify_special_char(). |
| 172 |
|
| -------------------------------------------------------------------------- */ |
| 173 |
|
| char * |
| 174 |
72 |
| bt_purify_string (const char *string, btshort options) |
| 175 |
|
| { |
| 176 |
72 |
| size_t src = 0; |
| 177 |
72 |
| int depth = 0; |
| 178 |
|
| bt_sbuf out; |
| 179 |
|
| |
| 180 |
|
| (void) options; |
| 181 |
72 |
| bt_sbuf_init (&out); |
| 182 |
72 |
| bt_sbuf_grow (&out, strlen (string)); |
| 183 |
|
| |
| 184 |
841 |
▶ | while (string[src] != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| while (string[src] != 0) |
769 |
72 |
841 |
|
| 185 |
|
| { |
| 186 |
769 |
| switch (string[src]) |
| 187 |
|
| { |
| 188 |
65 |
| case '~': /* "separator" characters -- */ |
| 189 |
|
| case '-': /* replaced with space */ |
| 190 |
|
| case ' ': /* and copy an actual space */ |
| 191 |
65 |
| bt_sbuf_putc (&out, ' '); |
| 192 |
65 |
| src++; |
| 193 |
65 |
| break; |
| 194 |
77 |
| case '{': |
| 195 |
77 |
▶ | if (depth == 0 && string[src + 1] == '\\') |
Branch: 2 branches
| Branch | True | False | Total |
| if (depth == 0 && string[src + 1] == '\\') |
59 |
18 |
77 |
| if (depth == 0 && string[src + 1] == '\\') |
50 |
9 |
59 |
|
| 196 |
50 |
| purify_special_char (string, &src, &out); |
| 197 |
|
| else |
| 198 |
27 |
| src++; |
| 199 |
77 |
| depth++; |
| 200 |
77 |
| break; |
| 201 |
77 |
| case '}': |
| 202 |
77 |
| depth--; |
| 203 |
77 |
| src++; |
| 204 |
77 |
| break; |
| 205 |
550 |
| default: |
| 206 |
|
| { |
| 207 |
550 |
| size_t n = charlen (string + src); |
| 208 |
550 |
▶ | if (bt_uni_isalnum (string + src) || bt_uni_ismark (string + src)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (bt_uni_isalnum (string + src) || bt_uni_ismark (string + src)) |
44 |
506 |
550 |
| if (bt_uni_isalnum (string + src) || bt_uni_ismark (string + src)) |
1 |
43 |
44 |
|
| 209 |
507 |
| bt_sbuf_putn (&out, string + src, n); |
| 210 |
550 |
| src += n; |
| 211 |
|
| } |
| 212 |
|
| } |
| 213 |
|
| } |
| 214 |
|
| |
| 215 |
72 |
| return bt_sbuf_steal (&out); |
| 216 |
|
| } |
| 217 |
|
| |
| 218 |
|
| |
| 219 |
|
| /* ====================================================================== |
| 220 |
|
| * Case-transformation stuff |
| 221 |
|
| */ |
| 222 |
|
| |
| 223 |
|
| static boolean |
| 224 |
336 |
| is_space (char c) |
| 225 |
|
| { |
| 226 |
306 |
▶ | return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' |
Branch: 4 branches
| Branch | True | False | Total |
| return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' |
306 |
0 |
306 |
| return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' |
306 |
0 |
306 |
| return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' |
306 |
0 |
306 |
| return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' |
306 |
0 |
306 |
|
| 227 |
642 |
▶ | || c == '\v'; |
Branch: 2 branches
| Branch | True | False | Total |
| || c == '\v'; |
306 |
30 |
336 |
| || c == '\v'; |
0 |
306 |
306 |
|
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
|
| /* ------------------------------------------------------------------------ |
| 231 |
|
| @NAME : convert_special_char() |
| 232 |
|
| @DESCRIPTION: Does case conversion on a special character: text at any |
| 233 |
|
| depth inside it is case-mangled (lowercased for 'l' and 't', |
| 234 |
|
| uppercased for 'u'); control sequences are left alone unless |
| 235 |
|
| they are foreign letters, which are converted using the |
| 236 |
|
| uc_version/lc_version tables. |
| 237 |
|
| -------------------------------------------------------------------------- */ |
| 238 |
|
| static void |
| 239 |
3 |
| convert_special_char (char transform, const char *string, size_t *src, |
| 240 |
|
| bt_sbuf *out, boolean *start_sentence, |
| 241 |
|
| boolean *after_colon) |
| 242 |
|
| { |
| 243 |
3 |
| int depth = 1; |
| 244 |
3 |
| boolean done_special = FALSE; |
| 245 |
|
| |
| 246 |
3 |
| bt_sbuf_putc (out, string[(*src)++]); /* copy the opening brace */ |
| 247 |
|
| |
| 248 |
11 |
▶ | while (string[*src] != 0 && !done_special) |
Branch: 2 branches
| Branch | True | False | Total |
| while (string[*src] != 0 && !done_special) |
11 |
0 |
11 |
| while (string[*src] != 0 && !done_special) |
8 |
3 |
11 |
|
| 249 |
|
| { |
| 250 |
8 |
| switch (string[*src]) |
| 251 |
|
| { |
| 252 |
3 |
| case '\\': /* a control sequence */ |
| 253 |
|
| { |
| 254 |
3 |
| size_t cs_end = *src + 1; |
| 255 |
|
| bt_letter letter; |
| 256 |
|
| |
| 257 |
7 |
▶ | while (BT_IS_ALPHA (string[cs_end])) |
Branch: 4 branches
| Branch | True | False | Total |
| while (BT_IS_ALPHA (string[cs_end])) |
6 |
1 |
7 |
| while (BT_IS_ALPHA (string[cs_end])) |
4 |
2 |
6 |
| while (BT_IS_ALPHA (string[cs_end])) |
2 |
1 |
3 |
| while (BT_IS_ALPHA (string[cs_end])) |
0 |
2 |
2 |
|
| 258 |
4 |
| cs_end++; |
| 259 |
|
| |
| 260 |
3 |
▶ | if (foreign_letter (string, (int) *src + 1, (int) cs_end, &letter)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (foreign_letter (string, (int) *src + 1, (int) cs_end, &letter)) |
2 |
1 |
3 |
|
| 261 |
|
| { |
| 262 |
|
| const char * repl; |
| 263 |
|
| |
| 264 |
2 |
▶ | if (transform == 'u') |
Branch: 1 branch
| Branch | True | False | Total |
| if (transform == 'u') |
2 |
0 |
2 |
|
| 265 |
2 |
| repl = uc_version[letter]; |
| 266 |
0 |
▶ | else if (transform == 'l') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (transform == 'l') |
0 |
0 |
0 |
|
| 267 |
0 |
| repl = lc_version[letter]; |
| 268 |
0 |
▶ | else if (*start_sentence || *after_colon) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (*start_sentence || *after_colon) |
0 |
0 |
0 |
| else if (*start_sentence || *after_colon) |
0 |
0 |
0 |
|
| 269 |
|
| { |
| 270 |
0 |
| repl = uc_version[letter]; |
| 271 |
0 |
| *start_sentence = *after_colon = FALSE; |
| 272 |
|
| } |
| 273 |
|
| else |
| 274 |
0 |
| repl = lc_version[letter]; |
| 275 |
2 |
| bt_sbuf_puts (out, repl); |
| 276 |
|
| } |
| 277 |
|
| else /* copy the control seq. as is */ |
| 278 |
1 |
| bt_sbuf_putn (out, string + *src, cs_end - *src); |
| 279 |
3 |
| *src = cs_end; |
| 280 |
3 |
| break; |
| 281 |
|
| } |
| 282 |
|
| |
| 283 |
0 |
| case '{': |
| 284 |
0 |
| bt_sbuf_putc (out, string[(*src)++]); |
| 285 |
0 |
| depth++; |
| 286 |
0 |
| break; |
| 287 |
|
| |
| 288 |
3 |
| case '}': |
| 289 |
3 |
| bt_sbuf_putc (out, string[(*src)++]); |
| 290 |
3 |
| depth--; |
| 291 |
3 |
▶ | if (depth == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth == 0) |
3 |
0 |
3 |
|
| 292 |
3 |
| done_special = TRUE; |
| 293 |
3 |
| break; |
| 294 |
|
| |
| 295 |
2 |
| default: |
| 296 |
|
| /* Inside special chars, lowercase and title caps are the same |
| 297 |
|
| * (BibTeX's convention). */ |
| 298 |
2 |
| *src += bt_uni_map_char (out, string + *src, |
| 299 |
|
| transform == 'u' |
| 300 |
|
| ? BT_CASE_UPPER : BT_CASE_LOWER); |
| 301 |
|
| } |
| 302 |
|
| } |
| 303 |
3 |
| } |
| 304 |
|
| |
| 305 |
|
| |
| 306 |
|
| /* ------------------------------------------------------------------------ |
| 307 |
|
| @NAME : bt_change_case() |
| 308 |
|
| @DESCRIPTION: Converts a string to uppercase ('u'), lowercase ('l'), or |
| 309 |
|
| "title capitalization" ('t': lowercase, except for the first |
| 310 |
|
| character of the string, of each sentence, and after a |
| 311 |
|
| colon). Text in braces is left alone, except in special |
| 312 |
|
| characters (see convert_special_char()). |
| 313 |
|
| @RETURNS : new string, or NULL (with a usage error) if `transform' is |
| 314 |
|
| invalid |
| 315 |
|
| -------------------------------------------------------------------------- */ |
| 316 |
|
| char * |
| 317 |
19 |
| bt_change_case (bt_errors *errs, char transform, const char *string, |
| 318 |
|
| btshort options) |
| 319 |
|
| { |
| 320 |
19 |
| size_t src = 0; |
| 321 |
19 |
| int depth = 0; |
| 322 |
19 |
| boolean start_sentence = TRUE; |
| 323 |
19 |
| boolean after_colon = FALSE; |
| 324 |
|
| bt_sbuf out; |
| 325 |
|
| |
| 326 |
|
| (void) options; |
| 327 |
19 |
▶ | if (transform != 'u' && transform != 'l' && transform != 't') |
Branch: 3 branches
| Branch | True | False | Total |
| if (transform != 'u' && transform != 'l' && transform != 't') |
14 |
5 |
19 |
| if (transform != 'u' && transform != 'l' && transform != 't') |
9 |
5 |
14 |
| if (transform != 'u' && transform != 'l' && transform != 't') |
1 |
8 |
9 |
|
| 328 |
|
| { |
| 329 |
1 |
| bt_report (errs, BTERR_USAGEERR, NULL, -1, NULL, -1, |
| 330 |
|
| "bt_change_case: invalid transform \"%c\" " |
| 331 |
|
| "(must be one of \"t\", \"u\" or \"l\")", transform); |
| 332 |
1 |
| return NULL; |
| 333 |
|
| } |
| 334 |
|
| |
| 335 |
18 |
| bt_sbuf_init (&out); |
| 336 |
18 |
| bt_sbuf_grow (&out, strlen (string)); |
| 337 |
|
| |
| 338 |
360 |
▶ | while (string[src] != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| while (string[src] != 0) |
342 |
18 |
360 |
|
| 339 |
|
| { |
| 340 |
342 |
| switch (string[src]) |
| 341 |
|
| { |
| 342 |
4 |
| case '{': |
| 343 |
|
| /* A special character is handled in one go; anything else in |
| 344 |
|
| * braces is protected from case-mangling, but still switches |
| 345 |
|
| * us out of start-of-sentence/after-colon mode. */ |
| 346 |
4 |
▶ | if (depth == 0 && string[src + 1] == '\\') |
Branch: 2 branches
| Branch | True | False | Total |
| if (depth == 0 && string[src + 1] == '\\') |
4 |
0 |
4 |
| if (depth == 0 && string[src + 1] == '\\') |
3 |
1 |
4 |
|
| 347 |
3 |
| convert_special_char (transform, string, &src, &out, |
| 348 |
|
| &start_sentence, &after_colon); |
| 349 |
|
| else |
| 350 |
|
| { |
| 351 |
1 |
| bt_sbuf_putc (&out, string[src++]); |
| 352 |
1 |
| start_sentence = after_colon = FALSE; |
| 353 |
1 |
| depth++; |
| 354 |
|
| } |
| 355 |
4 |
| break; |
| 356 |
|
| |
| 357 |
1 |
| case '}': |
| 358 |
1 |
| bt_sbuf_putc (&out, string[src++]); |
| 359 |
1 |
| depth--; |
| 360 |
1 |
| break; |
| 361 |
|
| |
| 362 |
|
| /* Sentence-ending punctuation and colons (BibTeX only cares |
| 363 |
|
| * about colons; we also capitalize the start of sentences) */ |
| 364 |
1 |
| case '.': |
| 365 |
|
| case '?': |
| 366 |
|
| case '!': |
| 367 |
1 |
| start_sentence = TRUE; |
| 368 |
1 |
| bt_sbuf_putc (&out, string[src++]); |
| 369 |
1 |
| break; |
| 370 |
|
| |
| 371 |
0 |
| case ':': |
| 372 |
0 |
| after_colon = TRUE; |
| 373 |
0 |
| bt_sbuf_putc (&out, string[src++]); |
| 374 |
0 |
| break; |
| 375 |
|
| |
| 376 |
336 |
| default: |
| 377 |
336 |
▶ | if (is_space (string[src])) |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_space (string[src])) |
30 |
306 |
336 |
|
| 378 |
30 |
| bt_sbuf_putc (&out, string[src++]); |
| 379 |
306 |
▶ | else if (depth == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (depth == 0) |
301 |
5 |
306 |
|
| 380 |
|
| { |
| 381 |
|
| bt_casemap how; |
| 382 |
|
| |
| 383 |
301 |
▶ | if (transform == 'u') |
Branch: 1 branch
| Branch | True | False | Total |
| if (transform == 'u') |
21 |
280 |
301 |
|
| 384 |
21 |
| how = BT_CASE_UPPER; |
| 385 |
280 |
▶ | else if (transform == 'l') |
Branch: 1 branch
| Branch | True | False | Total |
| else if (transform == 'l') |
24 |
256 |
280 |
|
| 386 |
24 |
| how = BT_CASE_LOWER; |
| 387 |
256 |
▶ | else if (start_sentence || after_colon) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (start_sentence || after_colon) |
247 |
9 |
256 |
| else if (start_sentence || after_colon) |
0 |
247 |
247 |
|
| 388 |
|
| { |
| 389 |
9 |
| how = BT_CASE_TITLE; |
| 390 |
9 |
| start_sentence = after_colon = FALSE; |
| 391 |
|
| } |
| 392 |
|
| else |
| 393 |
247 |
| how = BT_CASE_LOWER; |
| 394 |
301 |
| src += bt_uni_map_char (&out, string + src, how); |
| 395 |
|
| } |
| 396 |
|
| else |
| 397 |
|
| { |
| 398 |
5 |
| size_t n = charlen (string + src); |
| 399 |
5 |
| bt_sbuf_putn (&out, string + src, n); |
| 400 |
5 |
| src += n; |
| 401 |
|
| } |
| 402 |
|
| } |
| 403 |
|
| } |
| 404 |
|
| |
| 405 |
18 |
| return bt_sbuf_steal (&out); |
| 406 |
|
| } |