| 1 |
|
| /* ------------------------------------------------------------------------ |
| 2 |
|
| @NAME : format_name.c |
| 3 |
|
| @DESCRIPTION: bt_format_name() and support functions: everything needed |
| 4 |
|
| to turn a bt_name structure (as returned by bt_split_name()) |
| 5 |
|
| back into a string according to a highly customizable format. |
| 6 |
|
| @COPYRIGHT : Copyright (c) 1996-99 by Gregory P. Ward. All rights reserved. |
| 7 |
|
| |
| 8 |
|
| This file is part of the btparse library. This library is |
| 9 |
|
| free software; you can redistribute it and/or modify it under |
| 10 |
|
| the terms of the GNU Library General Public License as |
| 11 |
|
| published by the Free Software Foundation; either version 2 |
| 12 |
|
| of the License, or (at your option) any later version. |
| 13 |
|
| -------------------------------------------------------------------------- */ |
| 14 |
|
| |
| 15 |
|
| #include <stdlib.h> |
| 16 |
|
| #include <string.h> |
| 17 |
|
| #include "bt_internal.h" |
| 18 |
|
| |
| 19 |
|
| |
| 20 |
|
| /* ---------------------------------------------------------------------- |
| 21 |
|
| * Interface to create/customize bt_name_format structures |
| 22 |
|
| */ |
| 23 |
|
| |
| 24 |
|
| static void |
| 25 |
783 |
| set_text (char **slot, const char *text) |
| 26 |
|
| { |
| 27 |
783 |
| free (*slot); |
| 28 |
783 |
▶ | *slot = bt_xstrdup (text ? text : ""); |
Branch: 1 branch
| Branch | True | False | Total |
| *slot = bt_xstrdup (text ? text : ""); |
783 |
0 |
783 |
|
| 29 |
783 |
| } |
| 30 |
|
| |
| 31 |
|
| /* ------------------------------------------------------------------------ |
| 32 |
|
| @NAME : bt_create_name_format |
| 33 |
|
| @INPUT : parts - a string of letters (maximum four, from the set |
| 34 |
|
| f, v, l, j, with no repetition) denoting the order |
| 35 |
|
| and presence of name parts. |
| 36 |
|
| abbrev_first - flag: should first names be abbreviated? |
| 37 |
|
| @RETURNS : a new format (free with bt_free_name_format()), or NULL |
| 38 |
|
| (with a usage error) if `parts' is invalid |
| 39 |
|
| @DESCRIPTION: Sets token join methods: inter-token join (within each part) |
| 40 |
|
| is set to BTJ_MAYTIE (a "discretionary tie") for all parts; |
| 41 |
|
| inter-part join is set to BTJ_SPACE, except for a 'von' |
| 42 |
|
| token immediately preceding a 'last' token; there, we have |
| 43 |
|
| a discretionary tie. |
| 44 |
|
| |
| 45 |
|
| Sets abbreviation flags: FALSE for everything except `first', |
| 46 |
|
| which follows `abbrev_first' argument. |
| 47 |
|
| |
| 48 |
|
| Sets surrounding text (pre- and post-part, pre- and post- |
| 49 |
|
| token): empty string for everything, except: |
| 50 |
|
| - post-token for 'first' is "." if abbrev_first true |
| 51 |
|
| - if 'jr' immediately preceded by 'last': |
| 52 |
|
| pre-part for 'jr' is ", ", join for 'last' is nothing |
| 53 |
|
| - if 'first' immediately preceded by 'last' |
| 54 |
|
| pre-part for 'first' is ", " , join for 'last' is nothing |
| 55 |
|
| - if 'first' immediately preceded by 'jr' and 'jr' immediately |
| 56 |
|
| preceded by 'last': |
| 57 |
|
| pre-part for 'first' and 'jr' is ", " , |
| 58 |
|
| join for 'last' and 'jr' is nothing |
| 59 |
|
| -------------------------------------------------------------------------- */ |
| 60 |
|
| bt_name_format * |
| 61 |
43 |
| bt_create_name_format (bt_errors *errs, const char *parts, |
| 62 |
|
| boolean abbrev_first) |
| 63 |
|
| { |
| 64 |
|
| int num_parts; |
| 65 |
|
| int num_valid_parts; |
| 66 |
|
| bt_name_format * format; |
| 67 |
|
| int part_pos[BT_MAX_NAMEPARTS]; |
| 68 |
|
| int i; |
| 69 |
|
| |
| 70 |
215 |
▶ | for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
172 |
43 |
215 |
|
| 71 |
172 |
| part_pos[i] = -2; |
| 72 |
|
| |
| 73 |
43 |
| num_parts = (int) strlen (parts); |
| 74 |
43 |
| num_valid_parts = (int) strspn (parts, BT_VALID_NAMEPARTS); |
| 75 |
43 |
▶ | if (num_parts > BT_MAX_NAMEPARTS) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_parts > BT_MAX_NAMEPARTS) |
0 |
43 |
43 |
|
| 76 |
|
| { |
| 77 |
0 |
| bt_report (errs, BTERR_USAGEERR, NULL, -1, NULL, -1, |
| 78 |
|
| "bt_create_name_format: part list must have no more than " |
| 79 |
|
| "%d letters", BT_MAX_NAMEPARTS); |
| 80 |
0 |
| return NULL; |
| 81 |
|
| } |
| 82 |
43 |
▶ | if (num_valid_parts != num_parts) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_valid_parts != num_parts) |
0 |
43 |
43 |
|
| 83 |
|
| { |
| 84 |
0 |
| bt_report (errs, BTERR_USAGEERR, NULL, -1, NULL, -1, |
| 85 |
|
| "bt_create_name_format: bad part abbreviation \"%c\" " |
| 86 |
|
| "(must be one of \"%s\")", |
| 87 |
0 |
| parts[num_valid_parts], BT_VALID_NAMEPARTS); |
| 88 |
0 |
| return NULL; |
| 89 |
|
| } |
| 90 |
43 |
| format = bt_xcalloc (1, sizeof (bt_name_format)); |
| 91 |
43 |
| format->num_parts = num_parts; |
| 92 |
168 |
▶ | for (i = 0; i < num_parts; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < num_parts; i++) |
125 |
43 |
168 |
|
| 93 |
|
| { |
| 94 |
125 |
| switch (parts[i]) |
| 95 |
|
| { |
| 96 |
36 |
| case 'f': format->parts[i] = BTN_FIRST; break; |
| 97 |
28 |
| case 'v': format->parts[i] = BTN_VON; break; |
| 98 |
34 |
| case 'l': format->parts[i] = BTN_LAST; break; |
| 99 |
27 |
| case 'j': format->parts[i] = BTN_JR; break; |
| 100 |
|
| } |
| 101 |
125 |
| part_pos[format->parts[i]] = i; |
| 102 |
|
| } |
| 103 |
90 |
▶ | for (; i < BT_MAX_NAMEPARTS; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (; i < BT_MAX_NAMEPARTS; i++) |
47 |
43 |
90 |
|
| 104 |
47 |
| format->parts[i] = BTN_NONE; |
| 105 |
|
| |
| 106 |
|
| /* |
| 107 |
|
| * Token join methods: between tokens for all parts is a discretionary |
| 108 |
|
| * tie, and the join between parts is a space (except for 'von': if |
| 109 |
|
| * followed by 'last', we will have a discretionary tie). |
| 110 |
|
| */ |
| 111 |
215 |
▶ | for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
172 |
43 |
215 |
|
| 112 |
|
| { |
| 113 |
172 |
| format->join_tokens[i] = BTJ_MAYTIE; |
| 114 |
172 |
| format->join_part[i] = BTJ_SPACE; |
| 115 |
|
| } |
| 116 |
43 |
▶ | if (part_pos[BTN_VON] + 1 == part_pos[BTN_LAST]) |
Branch: 1 branch
| Branch | True | False | Total |
| if (part_pos[BTN_VON] + 1 == part_pos[BTN_LAST]) |
28 |
15 |
43 |
|
| 117 |
28 |
| format->join_part[BTN_VON] = BTJ_MAYTIE; |
| 118 |
|
| |
| 119 |
43 |
| format->abbrev[BTN_FIRST] = abbrev_first; |
| 120 |
43 |
| format->abbrev[BTN_VON] = FALSE; |
| 121 |
43 |
| format->abbrev[BTN_LAST] = FALSE; |
| 122 |
43 |
| format->abbrev[BTN_JR] = FALSE; |
| 123 |
|
| |
| 124 |
215 |
▶ | for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
172 |
43 |
215 |
|
| 125 |
|
| { |
| 126 |
172 |
| set_text (&format->pre_part[i], ""); |
| 127 |
172 |
| set_text (&format->post_part[i], ""); |
| 128 |
172 |
| set_text (&format->pre_token[i], ""); |
| 129 |
172 |
| set_text (&format->post_token[i], ""); |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
|
| /* abbreviated first name: "Blow J" -> "Blow J.", or "J Blow" -> "J. Blow" */ |
| 133 |
43 |
▶ | if (abbrev_first) |
Branch: 1 branch
| Branch | True | False | Total |
| if (abbrev_first) |
34 |
9 |
43 |
|
| 134 |
34 |
| set_text (&format->post_token[BTN_FIRST], "."); |
| 135 |
|
| |
| 136 |
|
| /* 'jr' after 'last': "Joe Blow Jr." -> "Joe Blow, Jr." */ |
| 137 |
43 |
▶ | if (part_pos[BTN_JR] == part_pos[BTN_LAST] + 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (part_pos[BTN_JR] == part_pos[BTN_LAST] + 1) |
27 |
16 |
43 |
|
| 138 |
|
| { |
| 139 |
27 |
| set_text (&format->pre_part[BTN_JR], ", "); |
| 140 |
27 |
| format->join_part[BTN_LAST] = BTJ_NOTHING; |
| 141 |
|
| /* 'first' after 'last' and 'jr': "Blow, Jr. Joe"->"Blow, Jr., Joe" */ |
| 142 |
27 |
▶ | if (part_pos[BTN_FIRST] == part_pos[BTN_JR] + 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (part_pos[BTN_FIRST] == part_pos[BTN_JR] + 1) |
23 |
4 |
27 |
|
| 143 |
|
| { |
| 144 |
23 |
| set_text (&format->pre_part[BTN_FIRST], ", "); |
| 145 |
23 |
| format->join_part[BTN_JR] = BTJ_NOTHING; |
| 146 |
|
| } |
| 147 |
|
| } |
| 148 |
|
| /* first after last: "Blow Joe" -> "Blow, Joe" */ |
| 149 |
43 |
▶ | if (part_pos[BTN_FIRST] == part_pos[BTN_LAST] + 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (part_pos[BTN_FIRST] == part_pos[BTN_LAST] + 1) |
0 |
43 |
43 |
|
| 150 |
|
| { |
| 151 |
0 |
| set_text (&format->pre_part[BTN_FIRST], ", "); |
| 152 |
0 |
| format->join_part[BTN_LAST] = BTJ_NOTHING; |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
43 |
| return format; |
| 156 |
|
| } |
| 157 |
|
| |
| 158 |
|
| void |
| 159 |
43 |
| bt_free_name_format (bt_name_format *format) |
| 160 |
|
| { |
| 161 |
|
| int i; |
| 162 |
|
| |
| 163 |
43 |
▶ | if (!format) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!format) |
0 |
43 |
43 |
|
| 164 |
0 |
| return; |
| 165 |
215 |
▶ | for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
172 |
43 |
215 |
|
| 166 |
|
| { |
| 167 |
172 |
| free (format->pre_part[i]); |
| 168 |
172 |
| free (format->post_part[i]); |
| 169 |
172 |
| free (format->pre_token[i]); |
| 170 |
172 |
| free (format->post_token[i]); |
| 171 |
|
| } |
| 172 |
43 |
| free (format); |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
|
| /* Sets the "surrounding text" for a name part; NULL leaves a slot alone. |
| 176 |
|
| * The strings are copied. */ |
| 177 |
|
| void |
| 178 |
11 |
| bt_set_format_text (bt_name_format *format, bt_namepart part, |
| 179 |
|
| const char *pre_part, const char *post_part, |
| 180 |
|
| const char *pre_token, const char *post_token) |
| 181 |
|
| { |
| 182 |
11 |
▶ | if ((int) part < 0 || part >= BT_MAX_NAMEPARTS) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((int) part < 0 || part >= BT_MAX_NAMEPARTS) |
11 |
0 |
11 |
| if ((int) part < 0 || part >= BT_MAX_NAMEPARTS) |
0 |
11 |
11 |
|
| 183 |
0 |
| return; |
| 184 |
11 |
▶ | if (pre_part) set_text (&format->pre_part[part], pre_part); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pre_part) set_text (&format->pre_part[part], pre_part); |
0 |
11 |
11 |
|
| 185 |
11 |
▶ | if (post_part) set_text (&format->post_part[part], post_part); |
Branch: 1 branch
| Branch | True | False | Total |
| if (post_part) set_text (&format->post_part[part], post_part); |
0 |
11 |
11 |
|
| 186 |
11 |
▶ | if (pre_token) set_text (&format->pre_token[part], pre_token); |
Branch: 1 branch
| Branch | True | False | Total |
| if (pre_token) set_text (&format->pre_token[part], pre_token); |
0 |
11 |
11 |
|
| 187 |
11 |
▶ | if (post_token) set_text (&format->post_token[part], post_token); |
Branch: 1 branch
| Branch | True | False | Total |
| if (post_token) set_text (&format->post_token[part], post_token); |
11 |
0 |
11 |
|
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| void |
| 191 |
14 |
| bt_set_format_options (bt_name_format *format, bt_namepart part, |
| 192 |
|
| boolean abbrev, bt_joinmethod join_tokens, |
| 193 |
|
| bt_joinmethod join_part) |
| 194 |
|
| { |
| 195 |
14 |
▶ | if ((int) part < 0 || part >= BT_MAX_NAMEPARTS) |
Branch: 2 branches
| Branch | True | False | Total |
| if ((int) part < 0 || part >= BT_MAX_NAMEPARTS) |
14 |
0 |
14 |
| if ((int) part < 0 || part >= BT_MAX_NAMEPARTS) |
0 |
14 |
14 |
|
| 196 |
0 |
| return; |
| 197 |
14 |
| format->abbrev[part] = abbrev; |
| 198 |
14 |
| format->join_tokens[part] = join_tokens; |
| 199 |
14 |
| format->join_part[part] = join_part; |
| 200 |
|
| } |
| 201 |
|
| |
| 202 |
|
| |
| 203 |
|
| /* ---------------------------------------------------------------------- |
| 204 |
|
| * Functions for actually formatting a name (given a name and a name |
| 205 |
|
| * format structure). |
| 206 |
|
| */ |
| 207 |
|
| |
| 208 |
|
| /* ------------------------------------------------------------------------ |
| 209 |
|
| @NAME : count_virtual_char() |
| 210 |
|
| @DESCRIPTION: Munches a single byte from a string, updating the virtual |
| 211 |
|
| character count, the depth, and an "in special character" |
| 212 |
|
| flag. |
| 213 |
|
| |
| 214 |
|
| The virtual character count is incremented by any character |
| 215 |
|
| (grapheme cluster) not part of a special character, and also |
| 216 |
|
| by the right-brace that closes a special character. The depth |
| 217 |
|
| is incremented by a left brace, and decremented by a right |
| 218 |
|
| brace. in_special is set to TRUE when we encounter a left |
| 219 |
|
| brace at depth zero that is immediately followed by a |
| 220 |
|
| backslash; it is set to false when we encounter the end of |
| 221 |
|
| the special character, i.e. when in_special is TRUE and we |
| 222 |
|
| hit a right brace that brings us back to depth zero. |
| 223 |
|
| |
| 224 |
|
| *glen tracks how many bytes remain of the current grapheme; |
| 225 |
|
| the count goes up on its last byte. Set all of the state to |
| 226 |
|
| zero/FALSE before the first call on a string. |
| 227 |
|
| -------------------------------------------------------------------------- */ |
| 228 |
|
| static void |
| 229 |
1886 |
| count_virtual_char (const char *string, int offset, int *vchar_count, |
| 230 |
|
| int *depth, boolean *in_special, int *glen) |
| 231 |
|
| { |
| 232 |
1886 |
| switch (string[offset]) |
| 233 |
|
| { |
| 234 |
50 |
| case '{': |
| 235 |
|
| /* start of a special char? */ |
| 236 |
50 |
▶ | if (*depth == 0 && string[offset + 1] == '\\') |
Branch: 2 branches
| Branch | True | False | Total |
| if (*depth == 0 && string[offset + 1] == '\\') |
48 |
2 |
50 |
| if (*depth == 0 && string[offset + 1] == '\\') |
32 |
16 |
48 |
|
| 237 |
32 |
| *in_special = TRUE; |
| 238 |
50 |
| (*depth)++; |
| 239 |
50 |
| break; |
| 240 |
47 |
| case '}': |
| 241 |
|
| /* end of a special char? */ |
| 242 |
47 |
▶ | if (*depth == 1 && *in_special) |
Branch: 2 branches
| Branch | True | False | Total |
| if (*depth == 1 && *in_special) |
31 |
16 |
47 |
| if (*depth == 1 && *in_special) |
24 |
7 |
31 |
|
| 243 |
|
| { |
| 244 |
24 |
| *in_special = FALSE; |
| 245 |
24 |
| (*vchar_count)++; |
| 246 |
|
| } |
| 247 |
47 |
| (*depth)--; |
| 248 |
47 |
| break; |
| 249 |
1789 |
| default: |
| 250 |
1789 |
▶ | if (! *in_special) |
Branch: 1 branch
| Branch | True | False | Total |
| if (! *in_special) |
1717 |
72 |
1789 |
|
| 251 |
|
| { |
| 252 |
1717 |
▶ | if (*glen == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*glen == 0) |
1680 |
37 |
1717 |
|
| 253 |
1680 |
| *glen = (int) bt_uni_grapheme_len (string + offset); |
| 254 |
1717 |
▶ | if (*glen == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*glen == 1) |
1661 |
56 |
1717 |
|
| 255 |
|
| { |
| 256 |
1661 |
| (*vchar_count)++; |
| 257 |
1661 |
| *glen = 0; |
| 258 |
|
| } |
| 259 |
56 |
▶ | else if (*glen > 1) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*glen > 1) |
56 |
0 |
56 |
|
| 260 |
56 |
| (*glen)--; |
| 261 |
|
| } |
| 262 |
|
| } |
| 263 |
1886 |
| } |
| 264 |
|
| |
| 265 |
|
| /* "virtual length" of a string: special characters count as one, as |
| 266 |
|
| * does each grapheme cluster outside them */ |
| 267 |
|
| static int |
| 268 |
114 |
| string_length (const char *string) |
| 269 |
|
| { |
| 270 |
114 |
| int length = 0, depth = 0, glen = 0, i; |
| 271 |
114 |
| boolean in_special = FALSE; |
| 272 |
|
| |
| 273 |
114 |
▶ | if (string == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (string == NULL) |
0 |
114 |
114 |
|
| 274 |
0 |
| return 0; |
| 275 |
669 |
▶ | for (i = 0; string[i] != 0; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; string[i] != 0; i++) |
555 |
114 |
669 |
|
| 276 |
555 |
| count_virtual_char (string, i, &length, &depth, &in_special, &glen); |
| 277 |
114 |
| return length; |
| 278 |
|
| } |
| 279 |
|
| |
| 280 |
|
| /* ------------------------------------------------------------------------ |
| 281 |
|
| @NAME : string_prefix() |
| 282 |
|
| @RETURNS : physical length of the prefix of `string' (starting at |
| 283 |
|
| prefix_start) with a virtual length of `prefix_len' |
| 284 |
|
| -------------------------------------------------------------------------- */ |
| 285 |
|
| static int |
| 286 |
81 |
| string_prefix (const char *string, int prefix_start, int prefix_len) |
| 287 |
|
| { |
| 288 |
81 |
| int i, vchars_seen = 0, depth = 0, glen = 0; |
| 289 |
81 |
| boolean in_special = FALSE; |
| 290 |
|
| |
| 291 |
111 |
▶ | for (i = prefix_start; string[i] != 0; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = prefix_start; string[i] != 0; i++) |
111 |
0 |
111 |
|
| 292 |
|
| { |
| 293 |
111 |
| count_virtual_char (string, i, &vchars_seen, &depth, &in_special, |
| 294 |
|
| &glen); |
| 295 |
111 |
▶ | if (vchars_seen == prefix_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (vchars_seen == prefix_len) |
81 |
30 |
111 |
|
| 296 |
81 |
| return (i + 1) - prefix_start; |
| 297 |
|
| } |
| 298 |
0 |
| return i - prefix_start; |
| 299 |
|
| } |
| 300 |
|
| |
| 301 |
|
| /* ------------------------------------------------------------------------ |
| 302 |
|
| @NAME : string_prefix_start() |
| 303 |
|
| @RETURNS : index where we need to start looking at name part when |
| 304 |
|
| abbreviating: if the character at `index' opens a brace |
| 305 |
|
| group that is not a special character (examples: |
| 306 |
|
| "{John Henry} Ford" or "{Some Organisation Inc.}"), the |
| 307 |
|
| initial is taken from just inside the brace. |
| 308 |
|
| -------------------------------------------------------------------------- */ |
| 309 |
|
| static int |
| 310 |
610 |
| string_prefix_start (const char *string, int index) |
| 311 |
|
| { |
| 312 |
610 |
| int vchars_seen = 0, depth = 0, glen = 0; |
| 313 |
610 |
| boolean in_special = FALSE; |
| 314 |
|
| |
| 315 |
610 |
| count_virtual_char (string, index, &vchars_seen, &depth, &in_special, |
| 316 |
|
| &glen); |
| 317 |
610 |
▶ | if (! in_special && depth == 1) |
Branch: 2 branches
| Branch | True | False | Total |
| if (! in_special && depth == 1) |
602 |
8 |
610 |
| if (! in_special && depth == 1) |
7 |
595 |
602 |
|
| 318 |
7 |
| return index + 1; |
| 319 |
603 |
| return index; |
| 320 |
|
| } |
| 321 |
|
| |
| 322 |
|
| /* Appends at most len bytes (all of them if len <= 0) of text+start */ |
| 323 |
|
| static void |
| 324 |
817 |
| append_text (bt_sbuf *out, const char *text, int start, int len) |
| 325 |
|
| { |
| 326 |
|
| size_t n; |
| 327 |
|
| |
| 328 |
817 |
▶ | if (text == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (text == NULL) |
0 |
817 |
817 |
|
| 329 |
0 |
| return; |
| 330 |
817 |
| n = strlen (text + start); |
| 331 |
817 |
▶ | if (len > 0 && (size_t) len < n) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len > 0 && (size_t) len < n) |
87 |
730 |
817 |
| if (len > 0 && (size_t) len < n) |
83 |
4 |
87 |
|
| 332 |
83 |
| n = (size_t) len; |
| 333 |
817 |
| bt_sbuf_putn (out, text + start, n); |
| 334 |
|
| } |
| 335 |
|
| |
| 336 |
|
| /* ------------------------------------------------------------------------ |
| 337 |
|
| @NAME : append_join |
| 338 |
|
| @DESCRIPTION: Appends a "join character" ('~' or ' ') or nothing according |
| 339 |
|
| to the join method and the `should_tie' flag (which decides |
| 340 |
|
| what a BTJ_MAYTIE becomes). |
| 341 |
|
| -------------------------------------------------------------------------- */ |
| 342 |
|
| static void |
| 343 |
126 |
| append_join (bt_sbuf *out, bt_joinmethod method, boolean should_tie) |
| 344 |
|
| { |
| 345 |
126 |
| switch (method) |
| 346 |
|
| { |
| 347 |
63 |
▶ | case BTJ_MAYTIE: bt_sbuf_putc (out, should_tie ? '~' : ' '); break; |
Branch: 1 branch
| Branch | True | False | Total |
| case BTJ_MAYTIE: bt_sbuf_putc (out, should_tie ? '~' : ' '); break; |
44 |
19 |
63 |
|
| 348 |
18 |
| case BTJ_SPACE: bt_sbuf_putc (out, ' '); break; |
| 349 |
9 |
| case BTJ_FORCETIE: bt_sbuf_putc (out, '~'); break; |
| 350 |
36 |
| case BTJ_NOTHING: break; |
| 351 |
|
| } |
| 352 |
126 |
| } |
| 353 |
|
| |
| 354 |
|
| |
| 355 |
|
| /* ------------------------------------------------------------------------ |
| 356 |
|
| @NAME : bt_format_name() |
| 357 |
|
| @INPUT : name, format |
| 358 |
|
| @RETURNS : formatted name (a malloc'd string) |
| 359 |
|
| -------------------------------------------------------------------------- */ |
| 360 |
|
| char * |
| 361 |
66 |
| bt_format_name (bt_errors *errs, bt_name *name, bt_name_format *format) |
| 362 |
|
| { |
| 363 |
|
| bt_namepart parts[BT_MAX_NAMEPARTS]; /* culled list from format */ |
| 364 |
|
| int num_parts; |
| 365 |
|
| char ** tokens[BT_MAX_NAMEPARTS]; |
| 366 |
|
| int num_tokens[BT_MAX_NAMEPARTS]; |
| 367 |
|
| bt_sbuf out; |
| 368 |
|
| int i, j, k; |
| 369 |
|
| bt_namepart part; |
| 370 |
66 |
| int token_vlen = -1; /* "virtual" length of last token */ |
| 371 |
|
| boolean should_tie; |
| 372 |
66 |
| boolean hyphen_todo = FALSE; |
| 373 |
|
| |
| 374 |
330 |
▶ | for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < BT_MAX_NAMEPARTS; i++) |
264 |
66 |
330 |
|
| 375 |
|
| { |
| 376 |
264 |
| tokens[i] = name->parts[i]; |
| 377 |
264 |
| num_tokens[i] = name->part_len[i]; |
| 378 |
|
| } |
| 379 |
|
| |
| 380 |
|
| /* |
| 381 |
|
| * Cull format->parts down by keeping only those parts that are actually |
| 382 |
|
| * present in the current name (keeps the main loop simpler: makes it |
| 383 |
|
| * easy to know if the "next part" is present or not, so we know whether |
| 384 |
|
| * to append a join character. |
| 385 |
|
| */ |
| 386 |
66 |
| num_parts = 0; |
| 387 |
265 |
▶ | for (i = 0; i < format->num_parts; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < format->num_parts; i++) |
199 |
66 |
265 |
|
| 388 |
|
| { |
| 389 |
199 |
| part = format->parts[i]; |
| 390 |
199 |
▶ | if (tokens[part]) |
Branch: 1 branch
| Branch | True | False | Total |
| if (tokens[part]) |
116 |
83 |
199 |
|
| 391 |
116 |
| parts[num_parts++] = part; |
| 392 |
|
| } |
| 393 |
|
| |
| 394 |
66 |
| bt_sbuf_init (&out); |
| 395 |
66 |
| bt_sbuf_grow (&out, 0); |
| 396 |
|
| |
| 397 |
182 |
▶ | for (i = 0; i < num_parts; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < num_parts; i++) |
116 |
66 |
182 |
|
| 398 |
|
| { |
| 399 |
116 |
| part = parts[i]; |
| 400 |
116 |
| append_text (&out, format->pre_part[part], 0, -1); |
| 401 |
|
| |
| 402 |
305 |
▶ | for (j = 0; j < num_tokens[part]; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < num_tokens[part]; j++) |
189 |
116 |
305 |
|
| 403 |
|
| { |
| 404 |
189 |
| const char * tok = tokens[part][j]; |
| 405 |
|
| |
| 406 |
189 |
▶ | if (!tok) /* ignore empty tokens */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tok) /* ignore empty tokens */ |
0 |
189 |
189 |
|
| 407 |
0 |
| continue; |
| 408 |
189 |
| append_text (&out, format->pre_token[part], 0, -1); |
| 409 |
|
| |
| 410 |
189 |
▶ | if (format->abbrev[part]) |
Branch: 1 branch
| Branch | True | False | Total |
| if (format->abbrev[part]) |
75 |
114 |
189 |
|
| 411 |
|
| { |
| 412 |
|
| /* Track depth and specials so we can ignore hyphenated token |
| 413 |
|
| * parts within protecting braces */ |
| 414 |
75 |
| int vchars_seen = 0, depth = 0, glen = 0; |
| 415 |
75 |
| boolean in_special = FALSE; |
| 416 |
|
| |
| 417 |
685 |
▶ | for (k = 0; tok[k] != 0; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; tok[k] != 0; k++) |
610 |
75 |
685 |
|
| 418 |
|
| { |
| 419 |
|
| int prefix_start; |
| 420 |
|
| |
| 421 |
610 |
| count_virtual_char (tok, k, &vchars_seen, &depth, |
| 422 |
|
| &in_special, &glen); |
| 423 |
610 |
| prefix_start = string_prefix_start (tok, k); |
| 424 |
|
| |
| 425 |
|
| /* Add initial from the beginning of the string, or after |
| 426 |
|
| * a hyphen */ |
| 427 |
610 |
▶ | if (k == 0 || hyphen_todo) |
Branch: 2 branches
| Branch | True | False | Total |
| if (k == 0 || hyphen_todo) |
535 |
75 |
610 |
| if (k == 0 || hyphen_todo) |
6 |
529 |
535 |
|
| 428 |
|
| { |
| 429 |
81 |
| append_text (&out, tok, prefix_start, |
| 430 |
|
| string_prefix (tok, prefix_start, 1)); |
| 431 |
81 |
| hyphen_todo = FALSE; |
| 432 |
|
| } |
| 433 |
|
| /* Potentially add a hyphen unless in protecting braces */ |
| 434 |
610 |
▶ | if (tok[k] == '-' && depth == 0 && in_special == FALSE) |
Branch: 3 branches
| Branch | True | False | Total |
| if (tok[k] == '-' && depth == 0 && in_special == FALSE) |
8 |
602 |
610 |
| if (tok[k] == '-' && depth == 0 && in_special == FALSE) |
6 |
2 |
8 |
| if (tok[k] == '-' && depth == 0 && in_special == FALSE) |
6 |
0 |
6 |
|
| 435 |
|
| { |
| 436 |
6 |
| append_text (&out, format->post_token[part], 0, -1); |
| 437 |
6 |
| append_text (&out, tok, k, 1); |
| 438 |
6 |
| hyphen_todo = TRUE; |
| 439 |
|
| } |
| 440 |
|
| } |
| 441 |
75 |
| token_vlen = 1; |
| 442 |
|
| } |
| 443 |
|
| else |
| 444 |
|
| { |
| 445 |
114 |
| append_text (&out, tok, 0, -1); |
| 446 |
114 |
| token_vlen = string_length (tok); |
| 447 |
|
| } |
| 448 |
|
| |
| 449 |
189 |
| append_text (&out, format->post_token[part], 0, -1); |
| 450 |
|
| |
| 451 |
|
| /* join to next token, but only if there is a next token! */ |
| 452 |
189 |
▶ | if (j < num_tokens[part] - 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (j < num_tokens[part] - 1) |
73 |
116 |
189 |
|
| 453 |
|
| { |
| 454 |
146 |
| should_tie = (num_tokens[part] > 1) |
| 455 |
110 |
▶ | && (((j == 0) && (token_vlen < 3)) |
Branch: 3 branches
| Branch | True | False | Total |
| && (((j == 0) && (token_vlen < 3)) |
73 |
0 |
73 |
| && (((j == 0) && (token_vlen < 3)) |
52 |
21 |
73 |
| && (((j == 0) && (token_vlen < 3)) |
16 |
36 |
52 |
|
| 456 |
37 |
▶ | || (j == num_tokens[part] - 2)); |
Branch: 1 branch
| Branch | True | False | Total |
| || (j == num_tokens[part] - 2)); |
25 |
12 |
37 |
|
| 457 |
73 |
| append_join (&out, format->join_tokens[part], should_tie); |
| 458 |
|
| } |
| 459 |
|
| } |
| 460 |
|
| |
| 461 |
116 |
| append_text (&out, format->post_part[part], 0, -1); |
| 462 |
|
| |
| 463 |
|
| /* join to the next part, but again only if there is a next part */ |
| 464 |
116 |
▶ | if (i < num_parts - 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < num_parts - 1) |
53 |
63 |
116 |
|
| 465 |
|
| { |
| 466 |
53 |
▶ | if (token_vlen == -1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (token_vlen == -1) |
0 |
53 |
53 |
|
| 467 |
|
| { |
| 468 |
0 |
| bt_report (errs, BTERR_INTERNAL, NULL, -1, NULL, -1, |
| 469 |
|
| "token_vlen uninitialized -- no tokens in a part " |
| 470 |
|
| "that I checked existed"); |
| 471 |
0 |
| token_vlen = 0; |
| 472 |
|
| } |
| 473 |
53 |
▶ | should_tie = (num_tokens[part] == 1 && token_vlen < 3); |
Branch: 2 branches
| Branch | True | False | Total |
| should_tie = (num_tokens[part] == 1 && token_vlen < 3); |
23 |
30 |
53 |
| should_tie = (num_tokens[part] == 1 && token_vlen < 3); |
2 |
21 |
23 |
|
| 474 |
53 |
| append_join (&out, format->join_part[part], should_tie); |
| 475 |
|
| } |
| 476 |
|
| } |
| 477 |
|
| |
| 478 |
66 |
| return bt_sbuf_steal (&out); |
| 479 |
|
| } |