Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * strings.c: string manipulation functions |
| 12 | */ |
| 13 | |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 14 | #define USING_FLOAT_STUFF |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 15 | #include "vim.h" |
| 16 | |
| 17 | /* |
| 18 | * Copy "string" into newly allocated memory. |
| 19 | */ |
| 20 | char_u * |
| 21 | vim_strsave(char_u *string) |
| 22 | { |
| 23 | char_u *p; |
| 24 | size_t len; |
| 25 | |
| 26 | len = STRLEN(string) + 1; |
| 27 | p = alloc(len); |
| 28 | if (p != NULL) |
| 29 | mch_memmove(p, string, len); |
| 30 | return p; |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * Copy up to "len" bytes of "string" into newly allocated memory and |
| 35 | * terminate with a NUL. |
| 36 | * The allocated memory always has size "len + 1", also when "string" is |
| 37 | * shorter. |
| 38 | */ |
| 39 | char_u * |
| 40 | vim_strnsave(char_u *string, size_t len) |
| 41 | { |
| 42 | char_u *p; |
| 43 | |
| 44 | p = alloc(len + 1); |
| 45 | if (p != NULL) |
| 46 | { |
| 47 | STRNCPY(p, string, len); |
| 48 | p[len] = NUL; |
| 49 | } |
| 50 | return p; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Same as vim_strsave(), but any characters found in esc_chars are preceded |
| 55 | * by a backslash. |
| 56 | */ |
| 57 | char_u * |
| 58 | vim_strsave_escaped(char_u *string, char_u *esc_chars) |
| 59 | { |
| 60 | return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE); |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape |
| 65 | * characters where rem_backslash() would remove the backslash. |
| 66 | * Escape the characters with "cc". |
| 67 | */ |
| 68 | char_u * |
| 69 | vim_strsave_escaped_ext( |
| 70 | char_u *string, |
| 71 | char_u *esc_chars, |
| 72 | int cc, |
| 73 | int bsl) |
| 74 | { |
| 75 | char_u *p; |
| 76 | char_u *p2; |
| 77 | char_u *escaped_string; |
| 78 | unsigned length; |
| 79 | int l; |
| 80 | |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 81 | // First count the number of backslashes required. |
| 82 | // Then allocate the memory and insert them. |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 83 | length = 1; // count the trailing NUL |
| 84 | for (p = string; *p; p++) |
| 85 | { |
| 86 | if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
| 87 | { |
| 88 | length += l; // count a multibyte char |
| 89 | p += l - 1; |
| 90 | continue; |
| 91 | } |
| 92 | if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p))) |
| 93 | ++length; // count a backslash |
| 94 | ++length; // count an ordinary char |
| 95 | } |
| 96 | escaped_string = alloc(length); |
| 97 | if (escaped_string != NULL) |
| 98 | { |
| 99 | p2 = escaped_string; |
| 100 | for (p = string; *p; p++) |
| 101 | { |
| 102 | if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
| 103 | { |
| 104 | mch_memmove(p2, p, (size_t)l); |
| 105 | p2 += l; |
| 106 | p += l - 1; // skip multibyte char |
| 107 | continue; |
| 108 | } |
| 109 | if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p))) |
| 110 | *p2++ = cc; |
| 111 | *p2++ = *p; |
| 112 | } |
| 113 | *p2 = NUL; |
| 114 | } |
| 115 | return escaped_string; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Return TRUE when 'shell' has "csh" in the tail. |
| 120 | */ |
| 121 | int |
| 122 | csh_like_shell(void) |
| 123 | { |
| 124 | return (strstr((char *)gettail(p_sh), "csh") != NULL); |
| 125 | } |
| 126 | |
| 127 | /* |
Jason Cox | 6e82351 | 2021-08-29 12:36:49 +0200 | [diff] [blame] | 128 | * Return TRUE when 'shell' has "fish" in the tail. |
| 129 | */ |
Dominique Pelle | de05ae7 | 2021-08-30 19:57:34 +0200 | [diff] [blame] | 130 | static int |
Jason Cox | 6e82351 | 2021-08-29 12:36:49 +0200 | [diff] [blame] | 131 | fish_like_shell(void) |
| 132 | { |
| 133 | return (strstr((char *)gettail(p_sh), "fish") != NULL); |
| 134 | } |
| 135 | |
| 136 | /* |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 137 | * Escape "string" for use as a shell argument with system(). |
| 138 | * This uses single quotes, except when we know we need to use double quotes |
| 139 | * (MS-DOS and MS-Windows not using PowerShell and without 'shellslash' set). |
| 140 | * PowerShell also uses a novel escaping for enclosed single quotes - double |
| 141 | * them up. |
| 142 | * Escape a newline, depending on the 'shell' option. |
| 143 | * When "do_special" is TRUE also replace "!", "%", "#" and things starting |
| 144 | * with "<" like "<cfile>". |
| 145 | * When "do_newline" is FALSE do not escape newline unless it is csh shell. |
| 146 | * Returns the result in allocated memory, NULL if we have run out. |
| 147 | */ |
| 148 | char_u * |
| 149 | vim_strsave_shellescape(char_u *string, int do_special, int do_newline) |
| 150 | { |
| 151 | unsigned length; |
| 152 | char_u *p; |
| 153 | char_u *d; |
| 154 | char_u *escaped_string; |
| 155 | int l; |
| 156 | int csh_like; |
Jason Cox | 6e82351 | 2021-08-29 12:36:49 +0200 | [diff] [blame] | 157 | int fish_like; |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 158 | char_u *shname; |
| 159 | int powershell; |
| 160 | # ifdef MSWIN |
| 161 | int double_quotes; |
| 162 | # endif |
| 163 | |
| 164 | // Only csh and similar shells expand '!' within single quotes. For sh and |
| 165 | // the like we must not put a backslash before it, it will be taken |
| 166 | // literally. If do_special is set the '!' will be escaped twice. |
| 167 | // Csh also needs to have "\n" escaped twice when do_special is set. |
| 168 | csh_like = csh_like_shell(); |
| 169 | |
Jason Cox | 6e82351 | 2021-08-29 12:36:49 +0200 | [diff] [blame] | 170 | // Fish shell uses '\' as an escape character within single quotes, so '\' |
| 171 | // itself must be escaped to get a literal '\'. |
| 172 | fish_like = fish_like_shell(); |
| 173 | |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 174 | // PowerShell uses its own version for quoting single quotes |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 175 | shname = gettail(p_sh); |
| 176 | powershell = strstr((char *)shname, "pwsh") != NULL; |
| 177 | # ifdef MSWIN |
| 178 | powershell = powershell || strstr((char *)shname, "powershell") != NULL; |
| 179 | // PowerShell only accepts single quotes so override shellslash. |
| 180 | double_quotes = !powershell && !p_ssl; |
| 181 | # endif |
| 182 | |
| 183 | // First count the number of extra bytes required. |
| 184 | length = (unsigned)STRLEN(string) + 3; // two quotes and a trailing NUL |
| 185 | for (p = string; *p != NUL; MB_PTR_ADV(p)) |
| 186 | { |
| 187 | # ifdef MSWIN |
| 188 | if (double_quotes) |
| 189 | { |
| 190 | if (*p == '"') |
| 191 | ++length; // " -> "" |
| 192 | } |
| 193 | else |
| 194 | # endif |
| 195 | if (*p == '\'') |
| 196 | { |
| 197 | if (powershell) |
| 198 | length +=2; // ' => '' |
| 199 | else |
| 200 | length += 3; // ' => '\'' |
| 201 | } |
| 202 | if ((*p == '\n' && (csh_like || do_newline)) |
| 203 | || (*p == '!' && (csh_like || do_special))) |
| 204 | { |
| 205 | ++length; // insert backslash |
| 206 | if (csh_like && do_special) |
| 207 | ++length; // insert backslash |
| 208 | } |
| 209 | if (do_special && find_cmdline_var(p, &l) >= 0) |
| 210 | { |
| 211 | ++length; // insert backslash |
| 212 | p += l - 1; |
| 213 | } |
Jason Cox | 6e82351 | 2021-08-29 12:36:49 +0200 | [diff] [blame] | 214 | if (*p == '\\' && fish_like) |
| 215 | ++length; // insert backslash |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Allocate memory for the result and fill it. |
| 219 | escaped_string = alloc(length); |
| 220 | if (escaped_string != NULL) |
| 221 | { |
| 222 | d = escaped_string; |
| 223 | |
| 224 | // add opening quote |
| 225 | # ifdef MSWIN |
| 226 | if (double_quotes) |
| 227 | *d++ = '"'; |
| 228 | else |
| 229 | # endif |
| 230 | *d++ = '\''; |
| 231 | |
| 232 | for (p = string; *p != NUL; ) |
| 233 | { |
| 234 | # ifdef MSWIN |
| 235 | if (double_quotes) |
| 236 | { |
| 237 | if (*p == '"') |
| 238 | { |
| 239 | *d++ = '"'; |
| 240 | *d++ = '"'; |
| 241 | ++p; |
| 242 | continue; |
| 243 | } |
| 244 | } |
| 245 | else |
| 246 | # endif |
| 247 | if (*p == '\'') |
| 248 | { |
| 249 | if (powershell) |
| 250 | { |
| 251 | *d++ = '\''; |
| 252 | *d++ = '\''; |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | *d++ = '\''; |
| 257 | *d++ = '\\'; |
| 258 | *d++ = '\''; |
| 259 | *d++ = '\''; |
| 260 | } |
| 261 | ++p; |
| 262 | continue; |
| 263 | } |
| 264 | if ((*p == '\n' && (csh_like || do_newline)) |
| 265 | || (*p == '!' && (csh_like || do_special))) |
| 266 | { |
| 267 | *d++ = '\\'; |
| 268 | if (csh_like && do_special) |
| 269 | *d++ = '\\'; |
| 270 | *d++ = *p++; |
| 271 | continue; |
| 272 | } |
| 273 | if (do_special && find_cmdline_var(p, &l) >= 0) |
| 274 | { |
| 275 | *d++ = '\\'; // insert backslash |
| 276 | while (--l >= 0) // copy the var |
| 277 | *d++ = *p++; |
| 278 | continue; |
| 279 | } |
Jason Cox | 6e82351 | 2021-08-29 12:36:49 +0200 | [diff] [blame] | 280 | if (*p == '\\' && fish_like) |
| 281 | { |
| 282 | *d++ = '\\'; |
| 283 | *d++ = *p++; |
Bram Moolenaar | 6631597 | 2021-09-01 14:31:51 +0200 | [diff] [blame] | 284 | continue; |
Jason Cox | 6e82351 | 2021-08-29 12:36:49 +0200 | [diff] [blame] | 285 | } |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 286 | |
| 287 | MB_COPY_CHAR(p, d); |
| 288 | } |
| 289 | |
| 290 | // add terminating quote and finish with a NUL |
| 291 | # ifdef MSWIN |
| 292 | if (double_quotes) |
| 293 | *d++ = '"'; |
| 294 | else |
| 295 | # endif |
| 296 | *d++ = '\''; |
| 297 | *d = NUL; |
| 298 | } |
| 299 | |
| 300 | return escaped_string; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Like vim_strsave(), but make all characters uppercase. |
| 305 | * This uses ASCII lower-to-upper case translation, language independent. |
| 306 | */ |
| 307 | char_u * |
| 308 | vim_strsave_up(char_u *string) |
| 309 | { |
| 310 | char_u *p1; |
| 311 | |
| 312 | p1 = vim_strsave(string); |
| 313 | vim_strup(p1); |
| 314 | return p1; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * Like vim_strnsave(), but make all characters uppercase. |
| 319 | * This uses ASCII lower-to-upper case translation, language independent. |
| 320 | */ |
| 321 | char_u * |
| 322 | vim_strnsave_up(char_u *string, size_t len) |
| 323 | { |
| 324 | char_u *p1; |
| 325 | |
| 326 | p1 = vim_strnsave(string, len); |
| 327 | vim_strup(p1); |
| 328 | return p1; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * ASCII lower-to-upper case translation, language independent. |
| 333 | */ |
| 334 | void |
| 335 | vim_strup( |
| 336 | char_u *p) |
| 337 | { |
| 338 | char_u *p2; |
| 339 | int c; |
| 340 | |
| 341 | if (p != NULL) |
| 342 | { |
| 343 | p2 = p; |
| 344 | while ((c = *p2) != NUL) |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 345 | *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20); |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | |
| 349 | #if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO) |
| 350 | /* |
| 351 | * Make string "s" all upper-case and return it in allocated memory. |
| 352 | * Handles multi-byte characters as well as possible. |
| 353 | * Returns NULL when out of memory. |
| 354 | */ |
| 355 | static char_u * |
| 356 | strup_save(char_u *orig) |
| 357 | { |
| 358 | char_u *p; |
| 359 | char_u *res; |
| 360 | |
| 361 | res = p = vim_strsave(orig); |
| 362 | |
| 363 | if (res != NULL) |
| 364 | while (*p != NUL) |
| 365 | { |
| 366 | int l; |
| 367 | |
| 368 | if (enc_utf8) |
| 369 | { |
| 370 | int c, uc; |
| 371 | int newl; |
| 372 | char_u *s; |
| 373 | |
| 374 | c = utf_ptr2char(p); |
| 375 | l = utf_ptr2len(p); |
| 376 | if (c == 0) |
| 377 | { |
| 378 | // overlong sequence, use only the first byte |
| 379 | c = *p; |
| 380 | l = 1; |
| 381 | } |
| 382 | uc = utf_toupper(c); |
| 383 | |
| 384 | // Reallocate string when byte count changes. This is rare, |
| 385 | // thus it's OK to do another malloc()/free(). |
| 386 | newl = utf_char2len(uc); |
| 387 | if (newl != l) |
| 388 | { |
| 389 | s = alloc(STRLEN(res) + 1 + newl - l); |
| 390 | if (s == NULL) |
| 391 | { |
| 392 | vim_free(res); |
| 393 | return NULL; |
| 394 | } |
| 395 | mch_memmove(s, res, p - res); |
| 396 | STRCPY(s + (p - res) + newl, p + l); |
| 397 | p = s + (p - res); |
| 398 | vim_free(res); |
| 399 | res = s; |
| 400 | } |
| 401 | |
| 402 | utf_char2bytes(uc, p); |
| 403 | p += newl; |
| 404 | } |
| 405 | else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
| 406 | p += l; // skip multi-byte character |
| 407 | else |
| 408 | { |
| 409 | *p = TOUPPER_LOC(*p); // note that toupper() can be a macro |
| 410 | p++; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | return res; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Make string "s" all lower-case and return it in allocated memory. |
| 419 | * Handles multi-byte characters as well as possible. |
| 420 | * Returns NULL when out of memory. |
| 421 | */ |
| 422 | char_u * |
| 423 | strlow_save(char_u *orig) |
| 424 | { |
| 425 | char_u *p; |
| 426 | char_u *res; |
| 427 | |
| 428 | res = p = vim_strsave(orig); |
| 429 | |
| 430 | if (res != NULL) |
| 431 | while (*p != NUL) |
| 432 | { |
| 433 | int l; |
| 434 | |
| 435 | if (enc_utf8) |
| 436 | { |
| 437 | int c, lc; |
| 438 | int newl; |
| 439 | char_u *s; |
| 440 | |
| 441 | c = utf_ptr2char(p); |
| 442 | l = utf_ptr2len(p); |
| 443 | if (c == 0) |
| 444 | { |
| 445 | // overlong sequence, use only the first byte |
| 446 | c = *p; |
| 447 | l = 1; |
| 448 | } |
| 449 | lc = utf_tolower(c); |
| 450 | |
| 451 | // Reallocate string when byte count changes. This is rare, |
| 452 | // thus it's OK to do another malloc()/free(). |
| 453 | newl = utf_char2len(lc); |
| 454 | if (newl != l) |
| 455 | { |
| 456 | s = alloc(STRLEN(res) + 1 + newl - l); |
| 457 | if (s == NULL) |
| 458 | { |
| 459 | vim_free(res); |
| 460 | return NULL; |
| 461 | } |
| 462 | mch_memmove(s, res, p - res); |
| 463 | STRCPY(s + (p - res) + newl, p + l); |
| 464 | p = s + (p - res); |
| 465 | vim_free(res); |
| 466 | res = s; |
| 467 | } |
| 468 | |
| 469 | utf_char2bytes(lc, p); |
| 470 | p += newl; |
| 471 | } |
| 472 | else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
| 473 | p += l; // skip multi-byte character |
| 474 | else |
| 475 | { |
| 476 | *p = TOLOWER_LOC(*p); // note that tolower() can be a macro |
| 477 | p++; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return res; |
| 482 | } |
| 483 | #endif |
| 484 | |
| 485 | /* |
| 486 | * delete spaces at the end of a string |
| 487 | */ |
| 488 | void |
| 489 | del_trailing_spaces(char_u *ptr) |
| 490 | { |
| 491 | char_u *q; |
| 492 | |
| 493 | q = ptr + STRLEN(ptr); |
| 494 | while (--q > ptr && VIM_ISWHITE(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V) |
| 495 | *q = NUL; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * Like strncpy(), but always terminate the result with one NUL. |
| 500 | * "to" must be "len + 1" long! |
| 501 | */ |
| 502 | void |
| 503 | vim_strncpy(char_u *to, char_u *from, size_t len) |
| 504 | { |
| 505 | STRNCPY(to, from, len); |
| 506 | to[len] = NUL; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Like strcat(), but make sure the result fits in "tosize" bytes and is |
| 511 | * always NUL terminated. "from" and "to" may overlap. |
| 512 | */ |
| 513 | void |
| 514 | vim_strcat(char_u *to, char_u *from, size_t tosize) |
| 515 | { |
| 516 | size_t tolen = STRLEN(to); |
| 517 | size_t fromlen = STRLEN(from); |
| 518 | |
| 519 | if (tolen + fromlen + 1 > tosize) |
| 520 | { |
| 521 | mch_memmove(to + tolen, from, tosize - tolen - 1); |
| 522 | to[tosize - 1] = NUL; |
| 523 | } |
| 524 | else |
| 525 | mch_memmove(to + tolen, from, fromlen + 1); |
| 526 | } |
| 527 | |
| 528 | #if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO) |
| 529 | /* |
| 530 | * Compare two strings, ignoring case, using current locale. |
| 531 | * Doesn't work for multi-byte characters. |
| 532 | * return 0 for match, < 0 for smaller, > 0 for bigger |
| 533 | */ |
| 534 | int |
| 535 | vim_stricmp(char *s1, char *s2) |
| 536 | { |
| 537 | int i; |
| 538 | |
| 539 | for (;;) |
| 540 | { |
| 541 | i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2); |
| 542 | if (i != 0) |
| 543 | return i; // this character different |
| 544 | if (*s1 == NUL) |
| 545 | break; // strings match until NUL |
| 546 | ++s1; |
| 547 | ++s2; |
| 548 | } |
| 549 | return 0; // strings match |
| 550 | } |
| 551 | #endif |
| 552 | |
| 553 | #if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO) |
| 554 | /* |
| 555 | * Compare two strings, for length "len", ignoring case, using current locale. |
| 556 | * Doesn't work for multi-byte characters. |
| 557 | * return 0 for match, < 0 for smaller, > 0 for bigger |
| 558 | */ |
| 559 | int |
| 560 | vim_strnicmp(char *s1, char *s2, size_t len) |
| 561 | { |
| 562 | int i; |
| 563 | |
| 564 | while (len > 0) |
| 565 | { |
| 566 | i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2); |
| 567 | if (i != 0) |
| 568 | return i; // this character different |
| 569 | if (*s1 == NUL) |
| 570 | break; // strings match until NUL |
| 571 | ++s1; |
| 572 | ++s2; |
| 573 | --len; |
| 574 | } |
| 575 | return 0; // strings match |
| 576 | } |
| 577 | #endif |
| 578 | |
| 579 | /* |
| 580 | * Search for first occurrence of "c" in "string". |
| 581 | * Version of strchr() that handles unsigned char strings with characters from |
| 582 | * 128 to 255 correctly. It also doesn't return a pointer to the NUL at the |
| 583 | * end of the string. |
| 584 | */ |
| 585 | char_u * |
| 586 | vim_strchr(char_u *string, int c) |
| 587 | { |
| 588 | char_u *p; |
| 589 | int b; |
| 590 | |
| 591 | p = string; |
| 592 | if (enc_utf8 && c >= 0x80) |
| 593 | { |
| 594 | while (*p != NUL) |
| 595 | { |
| 596 | int l = utfc_ptr2len(p); |
| 597 | |
| 598 | // Avoid matching an illegal byte here. |
| 599 | if (utf_ptr2char(p) == c && l > 1) |
| 600 | return p; |
| 601 | p += l; |
| 602 | } |
| 603 | return NULL; |
| 604 | } |
| 605 | if (enc_dbcs != 0 && c > 255) |
| 606 | { |
| 607 | int n2 = c & 0xff; |
| 608 | |
| 609 | c = ((unsigned)c >> 8) & 0xff; |
| 610 | while ((b = *p) != NUL) |
| 611 | { |
| 612 | if (b == c && p[1] == n2) |
| 613 | return p; |
| 614 | p += (*mb_ptr2len)(p); |
| 615 | } |
| 616 | return NULL; |
| 617 | } |
| 618 | if (has_mbyte) |
| 619 | { |
| 620 | while ((b = *p) != NUL) |
| 621 | { |
| 622 | if (b == c) |
| 623 | return p; |
| 624 | p += (*mb_ptr2len)(p); |
| 625 | } |
| 626 | return NULL; |
| 627 | } |
| 628 | while ((b = *p) != NUL) |
| 629 | { |
| 630 | if (b == c) |
| 631 | return p; |
| 632 | ++p; |
| 633 | } |
| 634 | return NULL; |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Version of strchr() that only works for bytes and handles unsigned char |
| 639 | * strings with characters above 128 correctly. It also doesn't return a |
| 640 | * pointer to the NUL at the end of the string. |
| 641 | */ |
| 642 | char_u * |
| 643 | vim_strbyte(char_u *string, int c) |
| 644 | { |
| 645 | char_u *p = string; |
| 646 | |
| 647 | while (*p != NUL) |
| 648 | { |
| 649 | if (*p == c) |
| 650 | return p; |
| 651 | ++p; |
| 652 | } |
| 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Search for last occurrence of "c" in "string". |
| 658 | * Version of strrchr() that handles unsigned char strings with characters from |
| 659 | * 128 to 255 correctly. It also doesn't return a pointer to the NUL at the |
| 660 | * end of the string. |
| 661 | * Return NULL if not found. |
| 662 | * Does not handle multi-byte char for "c"! |
| 663 | */ |
| 664 | char_u * |
| 665 | vim_strrchr(char_u *string, int c) |
| 666 | { |
| 667 | char_u *retval = NULL; |
| 668 | char_u *p = string; |
| 669 | |
| 670 | while (*p) |
| 671 | { |
| 672 | if (*p == c) |
| 673 | retval = p; |
| 674 | MB_PTR_ADV(p); |
| 675 | } |
| 676 | return retval; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Vim's version of strpbrk(), in case it's missing. |
| 681 | * Don't generate a prototype for this, causes problems when it's not used. |
| 682 | */ |
| 683 | #ifndef PROTO |
| 684 | # ifndef HAVE_STRPBRK |
| 685 | # ifdef vim_strpbrk |
| 686 | # undef vim_strpbrk |
| 687 | # endif |
| 688 | char_u * |
| 689 | vim_strpbrk(char_u *s, char_u *charset) |
| 690 | { |
| 691 | while (*s) |
| 692 | { |
| 693 | if (vim_strchr(charset, *s) != NULL) |
| 694 | return s; |
| 695 | MB_PTR_ADV(s); |
| 696 | } |
| 697 | return NULL; |
| 698 | } |
| 699 | # endif |
| 700 | #endif |
| 701 | |
| 702 | /* |
| 703 | * Sort an array of strings. |
| 704 | */ |
| 705 | static int sort_compare(const void *s1, const void *s2); |
| 706 | |
| 707 | static int |
| 708 | sort_compare(const void *s1, const void *s2) |
| 709 | { |
| 710 | return STRCMP(*(char **)s1, *(char **)s2); |
| 711 | } |
| 712 | |
| 713 | void |
| 714 | sort_strings( |
| 715 | char_u **files, |
| 716 | int count) |
| 717 | { |
| 718 | qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare); |
| 719 | } |
| 720 | |
| 721 | #if defined(FEAT_QUICKFIX) || defined(FEAT_SPELL) || defined(PROTO) |
| 722 | /* |
| 723 | * Return TRUE if string "s" contains a non-ASCII character (128 or higher). |
| 724 | * When "s" is NULL FALSE is returned. |
| 725 | */ |
| 726 | int |
| 727 | has_non_ascii(char_u *s) |
| 728 | { |
| 729 | char_u *p; |
| 730 | |
| 731 | if (s != NULL) |
| 732 | for (p = s; *p != NUL; ++p) |
| 733 | if (*p >= 128) |
| 734 | return TRUE; |
| 735 | return FALSE; |
| 736 | } |
| 737 | #endif |
| 738 | |
| 739 | /* |
| 740 | * Concatenate two strings and return the result in allocated memory. |
| 741 | * Returns NULL when out of memory. |
| 742 | */ |
| 743 | char_u * |
| 744 | concat_str(char_u *str1, char_u *str2) |
| 745 | { |
| 746 | char_u *dest; |
| 747 | size_t l = str1 == NULL ? 0 : STRLEN(str1); |
| 748 | |
| 749 | dest = alloc(l + (str2 == NULL ? 0 : STRLEN(str2)) + 1L); |
| 750 | if (dest != NULL) |
| 751 | { |
| 752 | if (str1 == NULL) |
| 753 | *dest = NUL; |
| 754 | else |
| 755 | STRCPY(dest, str1); |
| 756 | if (str2 != NULL) |
| 757 | STRCPY(dest + l, str2); |
| 758 | } |
| 759 | return dest; |
| 760 | } |
| 761 | |
| 762 | #if defined(FEAT_EVAL) || defined(PROTO) |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 763 | /* |
| 764 | * Return string "str" in ' quotes, doubling ' characters. |
| 765 | * If "str" is NULL an empty string is assumed. |
| 766 | * If "function" is TRUE make it function('string'). |
| 767 | */ |
| 768 | char_u * |
| 769 | string_quote(char_u *str, int function) |
| 770 | { |
| 771 | unsigned len; |
| 772 | char_u *p, *r, *s; |
| 773 | |
| 774 | len = (function ? 13 : 3); |
| 775 | if (str != NULL) |
| 776 | { |
| 777 | len += (unsigned)STRLEN(str); |
| 778 | for (p = str; *p != NUL; MB_PTR_ADV(p)) |
| 779 | if (*p == '\'') |
| 780 | ++len; |
| 781 | } |
| 782 | s = r = alloc(len); |
| 783 | if (r != NULL) |
| 784 | { |
| 785 | if (function) |
| 786 | { |
| 787 | STRCPY(r, "function('"); |
| 788 | r += 10; |
| 789 | } |
| 790 | else |
| 791 | *r++ = '\''; |
| 792 | if (str != NULL) |
| 793 | for (p = str; *p != NUL; ) |
| 794 | { |
| 795 | if (*p == '\'') |
| 796 | *r++ = '\''; |
| 797 | MB_COPY_CHAR(p, r); |
| 798 | } |
| 799 | *r++ = '\''; |
| 800 | if (function) |
| 801 | *r++ = ')'; |
| 802 | *r++ = NUL; |
| 803 | } |
| 804 | return s; |
| 805 | } |
| 806 | |
Yegappan Lakshmanan | f973eeb | 2021-12-22 18:19:26 +0000 | [diff] [blame] | 807 | /* |
| 808 | * Count the number of times "needle" occurs in string "haystack". Case is |
| 809 | * ignored if "ic" is TRUE. |
| 810 | */ |
| 811 | long |
| 812 | string_count(char_u *haystack, char_u *needle, int ic) |
| 813 | { |
| 814 | long n = 0; |
| 815 | char_u *p = haystack; |
| 816 | char_u *next; |
| 817 | |
| 818 | if (p == NULL || needle == NULL || *needle == NUL) |
| 819 | return 0; |
| 820 | |
| 821 | if (ic) |
| 822 | { |
| 823 | size_t len = STRLEN(needle); |
| 824 | |
| 825 | while (*p != NUL) |
| 826 | { |
| 827 | if (MB_STRNICMP(p, needle, len) == 0) |
| 828 | { |
| 829 | ++n; |
| 830 | p += len; |
| 831 | } |
| 832 | else |
| 833 | MB_PTR_ADV(p); |
| 834 | } |
| 835 | } |
| 836 | else |
| 837 | while ((next = (char_u *)strstr((char *)p, (char *)needle)) != NULL) |
| 838 | { |
| 839 | ++n; |
| 840 | p = next + STRLEN(needle); |
| 841 | } |
| 842 | |
| 843 | return n; |
| 844 | } |
| 845 | |
| 846 | /* |
| 847 | * Make a typval_T of the first character of "input" and store it in "output". |
| 848 | * Return OK or FAIL. |
| 849 | */ |
| 850 | static int |
| 851 | copy_first_char_to_tv(char_u *input, typval_T *output) |
| 852 | { |
| 853 | char_u buf[MB_MAXBYTES + 1]; |
| 854 | int len; |
| 855 | |
| 856 | if (input == NULL || output == NULL) |
| 857 | return FAIL; |
| 858 | |
| 859 | len = has_mbyte ? mb_ptr2len(input) : 1; |
| 860 | STRNCPY(buf, input, len); |
| 861 | buf[len] = NUL; |
| 862 | output->v_type = VAR_STRING; |
| 863 | output->vval.v_string = vim_strsave(buf); |
| 864 | |
| 865 | return output->vval.v_string == NULL ? FAIL : OK; |
| 866 | } |
| 867 | |
| 868 | /* |
| 869 | * Implementation of map() and filter() for a String. Apply "expr" to every |
| 870 | * character in string "str" and return the result in "rettv". |
| 871 | */ |
| 872 | void |
| 873 | string_filter_map( |
| 874 | char_u *str, |
| 875 | filtermap_T filtermap, |
| 876 | typval_T *expr, |
| 877 | typval_T *rettv) |
| 878 | { |
| 879 | char_u *p; |
| 880 | typval_T tv; |
| 881 | garray_T ga; |
| 882 | int len = 0; |
| 883 | int idx = 0; |
| 884 | int rem; |
| 885 | |
| 886 | rettv->v_type = VAR_STRING; |
| 887 | rettv->vval.v_string = NULL; |
| 888 | |
| 889 | // set_vim_var_nr() doesn't set the type |
| 890 | set_vim_var_type(VV_KEY, VAR_NUMBER); |
| 891 | |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 892 | ga_init2(&ga, sizeof(char), 80); |
Yegappan Lakshmanan | f973eeb | 2021-12-22 18:19:26 +0000 | [diff] [blame] | 893 | for (p = str; *p != NUL; p += len) |
| 894 | { |
| 895 | typval_T newtv; |
| 896 | |
| 897 | if (copy_first_char_to_tv(p, &tv) == FAIL) |
| 898 | break; |
| 899 | len = (int)STRLEN(tv.vval.v_string); |
| 900 | |
Bram Moolenaar | dd7eff0 | 2022-05-06 11:02:05 +0100 | [diff] [blame] | 901 | newtv.v_type = VAR_UNKNOWN; |
Yegappan Lakshmanan | f973eeb | 2021-12-22 18:19:26 +0000 | [diff] [blame] | 902 | set_vim_var_nr(VV_KEY, idx); |
| 903 | if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL |
| 904 | || did_emsg) |
Yegappan Lakshmanan | f973eeb | 2021-12-22 18:19:26 +0000 | [diff] [blame] | 905 | { |
| 906 | clear_tv(&newtv); |
| 907 | clear_tv(&tv); |
| 908 | break; |
| 909 | } |
| 910 | else if (filtermap != FILTERMAP_FILTER) |
| 911 | { |
| 912 | if (newtv.v_type != VAR_STRING) |
| 913 | { |
| 914 | clear_tv(&newtv); |
| 915 | clear_tv(&tv); |
Bram Moolenaar | e70cec9 | 2022-01-01 14:25:55 +0000 | [diff] [blame] | 916 | emsg(_(e_string_required)); |
Yegappan Lakshmanan | f973eeb | 2021-12-22 18:19:26 +0000 | [diff] [blame] | 917 | break; |
| 918 | } |
| 919 | else |
| 920 | ga_concat(&ga, newtv.vval.v_string); |
| 921 | } |
| 922 | else if (!rem) |
| 923 | ga_concat(&ga, tv.vval.v_string); |
| 924 | |
| 925 | clear_tv(&newtv); |
| 926 | clear_tv(&tv); |
| 927 | |
| 928 | ++idx; |
| 929 | } |
| 930 | ga_append(&ga, NUL); |
| 931 | rettv->vval.v_string = ga.ga_data; |
| 932 | } |
| 933 | |
| 934 | /* |
Bram Moolenaar | f1c60d4 | 2022-09-22 17:07:00 +0100 | [diff] [blame] | 935 | * Implementation of reduce() for String "argvars[0]" using the function "expr" |
| 936 | * starting with the optional initial value "argvars[2]" and return the result |
| 937 | * in "rettv". |
Yegappan Lakshmanan | f973eeb | 2021-12-22 18:19:26 +0000 | [diff] [blame] | 938 | */ |
| 939 | void |
| 940 | string_reduce( |
| 941 | typval_T *argvars, |
Bram Moolenaar | f1c60d4 | 2022-09-22 17:07:00 +0100 | [diff] [blame] | 942 | typval_T *expr, |
Yegappan Lakshmanan | f973eeb | 2021-12-22 18:19:26 +0000 | [diff] [blame] | 943 | typval_T *rettv) |
| 944 | { |
| 945 | char_u *p = tv_get_string(&argvars[0]); |
| 946 | int len; |
| 947 | typval_T argv[3]; |
| 948 | int r; |
| 949 | int called_emsg_start = called_emsg; |
| 950 | |
| 951 | if (argvars[2].v_type == VAR_UNKNOWN) |
| 952 | { |
| 953 | if (*p == NUL) |
| 954 | { |
Bram Moolenaar | e70cec9 | 2022-01-01 14:25:55 +0000 | [diff] [blame] | 955 | semsg(_(e_reduce_of_an_empty_str_with_no_initial_value), "String"); |
Yegappan Lakshmanan | f973eeb | 2021-12-22 18:19:26 +0000 | [diff] [blame] | 956 | return; |
| 957 | } |
| 958 | if (copy_first_char_to_tv(p, rettv) == FAIL) |
| 959 | return; |
| 960 | p += STRLEN(rettv->vval.v_string); |
| 961 | } |
Yegappan Lakshmanan | 8deb2b3 | 2022-09-02 15:15:27 +0100 | [diff] [blame] | 962 | else if (check_for_string_arg(argvars, 2) == FAIL) |
Yegappan Lakshmanan | f973eeb | 2021-12-22 18:19:26 +0000 | [diff] [blame] | 963 | return; |
Yegappan Lakshmanan | f973eeb | 2021-12-22 18:19:26 +0000 | [diff] [blame] | 964 | else |
| 965 | copy_tv(&argvars[2], rettv); |
| 966 | |
| 967 | for ( ; *p != NUL; p += len) |
| 968 | { |
| 969 | argv[0] = *rettv; |
| 970 | if (copy_first_char_to_tv(p, &argv[1]) == FAIL) |
| 971 | break; |
| 972 | len = (int)STRLEN(argv[1].vval.v_string); |
Bram Moolenaar | f1c60d4 | 2022-09-22 17:07:00 +0100 | [diff] [blame] | 973 | |
| 974 | r = eval_expr_typval(expr, argv, 2, rettv); |
| 975 | |
Yegappan Lakshmanan | f973eeb | 2021-12-22 18:19:26 +0000 | [diff] [blame] | 976 | clear_tv(&argv[0]); |
| 977 | clear_tv(&argv[1]); |
| 978 | if (r == FAIL || called_emsg != called_emsg_start) |
| 979 | return; |
| 980 | } |
| 981 | } |
| 982 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 983 | static void |
| 984 | byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED) |
| 985 | { |
| 986 | char_u *t; |
| 987 | char_u *str; |
| 988 | varnumber_T idx; |
| 989 | |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 990 | rettv->vval.v_number = -1; |
| 991 | |
| 992 | if (in_vim9script() |
| 993 | && (check_for_string_arg(argvars, 0) == FAIL |
| 994 | || check_for_number_arg(argvars, 1) == FAIL)) |
| 995 | return; |
| 996 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 997 | str = tv_get_string_chk(&argvars[0]); |
| 998 | idx = tv_get_number_chk(&argvars[1], NULL); |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 999 | if (str == NULL || idx < 0) |
| 1000 | return; |
| 1001 | |
| 1002 | t = str; |
| 1003 | for ( ; idx > 0; idx--) |
| 1004 | { |
| 1005 | if (*t == NUL) // EOL reached |
| 1006 | return; |
| 1007 | if (enc_utf8 && comp) |
| 1008 | t += utf_ptr2len(t); |
| 1009 | else |
| 1010 | t += (*mb_ptr2len)(t); |
| 1011 | } |
| 1012 | rettv->vval.v_number = (varnumber_T)(t - str); |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * "byteidx()" function |
| 1017 | */ |
| 1018 | void |
| 1019 | f_byteidx(typval_T *argvars, typval_T *rettv) |
| 1020 | { |
| 1021 | byteidx(argvars, rettv, FALSE); |
| 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * "byteidxcomp()" function |
| 1026 | */ |
| 1027 | void |
| 1028 | f_byteidxcomp(typval_T *argvars, typval_T *rettv) |
| 1029 | { |
| 1030 | byteidx(argvars, rettv, TRUE); |
| 1031 | } |
| 1032 | |
| 1033 | /* |
| 1034 | * "charidx()" function |
| 1035 | */ |
| 1036 | void |
| 1037 | f_charidx(typval_T *argvars, typval_T *rettv) |
| 1038 | { |
| 1039 | char_u *str; |
| 1040 | varnumber_T idx; |
| 1041 | varnumber_T countcc = FALSE; |
| 1042 | char_u *p; |
| 1043 | int len; |
| 1044 | int (*ptr2len)(char_u *); |
| 1045 | |
| 1046 | rettv->vval.v_number = -1; |
| 1047 | |
Yegappan Lakshmanan | 8deb2b3 | 2022-09-02 15:15:27 +0100 | [diff] [blame] | 1048 | if ((check_for_string_arg(argvars, 0) == FAIL |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1049 | || check_for_number_arg(argvars, 1) == FAIL |
| 1050 | || check_for_opt_bool_arg(argvars, 2) == FAIL)) |
| 1051 | return; |
| 1052 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1053 | str = tv_get_string_chk(&argvars[0]); |
| 1054 | idx = tv_get_number_chk(&argvars[1], NULL); |
| 1055 | if (str == NULL || idx < 0) |
| 1056 | return; |
| 1057 | |
| 1058 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1059 | countcc = tv_get_bool(&argvars[2]); |
| 1060 | if (countcc < 0 || countcc > 1) |
| 1061 | { |
| 1062 | semsg(_(e_using_number_as_bool_nr), countcc); |
| 1063 | return; |
| 1064 | } |
| 1065 | |
| 1066 | if (enc_utf8 && countcc) |
| 1067 | ptr2len = utf_ptr2len; |
| 1068 | else |
| 1069 | ptr2len = mb_ptr2len; |
| 1070 | |
| 1071 | for (p = str, len = 0; p <= str + idx; len++) |
| 1072 | { |
| 1073 | if (*p == NUL) |
| 1074 | return; |
| 1075 | p += ptr2len(p); |
| 1076 | } |
| 1077 | |
| 1078 | rettv->vval.v_number = len > 0 ? len - 1 : 0; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * "str2list()" function |
| 1083 | */ |
| 1084 | void |
| 1085 | f_str2list(typval_T *argvars, typval_T *rettv) |
| 1086 | { |
| 1087 | char_u *p; |
| 1088 | int utf8 = FALSE; |
| 1089 | |
| 1090 | if (rettv_list_alloc(rettv) == FAIL) |
| 1091 | return; |
| 1092 | |
Yegappan Lakshmanan | a9a7c0c | 2021-07-17 19:11:07 +0200 | [diff] [blame] | 1093 | if (in_vim9script() |
| 1094 | && (check_for_string_arg(argvars, 0) == FAIL |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1095 | || check_for_opt_bool_arg(argvars, 1) == FAIL)) |
Yegappan Lakshmanan | a9a7c0c | 2021-07-17 19:11:07 +0200 | [diff] [blame] | 1096 | return; |
| 1097 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1098 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1099 | utf8 = (int)tv_get_bool_chk(&argvars[1], NULL); |
| 1100 | |
| 1101 | p = tv_get_string(&argvars[0]); |
| 1102 | |
| 1103 | if (has_mbyte || utf8) |
| 1104 | { |
| 1105 | int (*ptr2len)(char_u *); |
| 1106 | int (*ptr2char)(char_u *); |
| 1107 | |
| 1108 | if (utf8 || enc_utf8) |
| 1109 | { |
| 1110 | ptr2len = utf_ptr2len; |
| 1111 | ptr2char = utf_ptr2char; |
| 1112 | } |
| 1113 | else |
| 1114 | { |
| 1115 | ptr2len = mb_ptr2len; |
| 1116 | ptr2char = mb_ptr2char; |
| 1117 | } |
| 1118 | |
| 1119 | for ( ; *p != NUL; p += (*ptr2len)(p)) |
| 1120 | list_append_number(rettv->vval.v_list, (*ptr2char)(p)); |
| 1121 | } |
| 1122 | else |
| 1123 | for ( ; *p != NUL; ++p) |
| 1124 | list_append_number(rettv->vval.v_list, *p); |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * "str2nr()" function |
| 1129 | */ |
| 1130 | void |
| 1131 | f_str2nr(typval_T *argvars, typval_T *rettv) |
| 1132 | { |
| 1133 | int base = 10; |
| 1134 | char_u *p; |
| 1135 | varnumber_T n; |
| 1136 | int what = 0; |
| 1137 | int isneg; |
| 1138 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1139 | if (in_vim9script() |
| 1140 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1141 | || check_for_opt_number_arg(argvars, 1) == FAIL |
| 1142 | || (argvars[1].v_type != VAR_UNKNOWN |
| 1143 | && check_for_opt_bool_arg(argvars, 2) == FAIL))) |
| 1144 | return; |
| 1145 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1146 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1147 | { |
| 1148 | base = (int)tv_get_number(&argvars[1]); |
| 1149 | if (base != 2 && base != 8 && base != 10 && base != 16) |
| 1150 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1151 | emsg(_(e_invalid_argument)); |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1152 | return; |
| 1153 | } |
| 1154 | if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2])) |
| 1155 | what |= STR2NR_QUOTE; |
| 1156 | } |
| 1157 | |
| 1158 | p = skipwhite(tv_get_string_strict(&argvars[0])); |
| 1159 | isneg = (*p == '-'); |
| 1160 | if (*p == '+' || *p == '-') |
| 1161 | p = skipwhite(p + 1); |
| 1162 | switch (base) |
| 1163 | { |
| 1164 | case 2: what |= STR2NR_BIN + STR2NR_FORCE; break; |
| 1165 | case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break; |
| 1166 | case 16: what |= STR2NR_HEX + STR2NR_FORCE; break; |
| 1167 | } |
| 1168 | vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE); |
| 1169 | // Text after the number is silently ignored. |
| 1170 | if (isneg) |
| 1171 | rettv->vval.v_number = -n; |
| 1172 | else |
| 1173 | rettv->vval.v_number = n; |
| 1174 | |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * "strgetchar()" function |
| 1179 | */ |
| 1180 | void |
| 1181 | f_strgetchar(typval_T *argvars, typval_T *rettv) |
| 1182 | { |
| 1183 | char_u *str; |
| 1184 | int len; |
| 1185 | int error = FALSE; |
| 1186 | int charidx; |
| 1187 | int byteidx = 0; |
| 1188 | |
| 1189 | rettv->vval.v_number = -1; |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 1190 | |
| 1191 | if (in_vim9script() |
| 1192 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1193 | || check_for_number_arg(argvars, 1) == FAIL)) |
| 1194 | return; |
| 1195 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1196 | str = tv_get_string_chk(&argvars[0]); |
| 1197 | if (str == NULL) |
| 1198 | return; |
| 1199 | len = (int)STRLEN(str); |
| 1200 | charidx = (int)tv_get_number_chk(&argvars[1], &error); |
| 1201 | if (error) |
| 1202 | return; |
| 1203 | |
| 1204 | while (charidx >= 0 && byteidx < len) |
| 1205 | { |
| 1206 | if (charidx == 0) |
| 1207 | { |
| 1208 | rettv->vval.v_number = mb_ptr2char(str + byteidx); |
| 1209 | break; |
| 1210 | } |
| 1211 | --charidx; |
| 1212 | byteidx += MB_CPTR2LEN(str + byteidx); |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | * "stridx()" function |
| 1218 | */ |
| 1219 | void |
| 1220 | f_stridx(typval_T *argvars, typval_T *rettv) |
| 1221 | { |
| 1222 | char_u buf[NUMBUFLEN]; |
| 1223 | char_u *needle; |
| 1224 | char_u *haystack; |
| 1225 | char_u *save_haystack; |
| 1226 | char_u *pos; |
| 1227 | int start_idx; |
| 1228 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1229 | if (in_vim9script() |
| 1230 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1231 | || check_for_string_arg(argvars, 1) == FAIL |
| 1232 | || check_for_opt_number_arg(argvars, 2) == FAIL)) |
| 1233 | return; |
| 1234 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1235 | needle = tv_get_string_chk(&argvars[1]); |
| 1236 | save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf); |
| 1237 | rettv->vval.v_number = -1; |
| 1238 | if (needle == NULL || haystack == NULL) |
| 1239 | return; // type error; errmsg already given |
| 1240 | |
| 1241 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1242 | { |
| 1243 | int error = FALSE; |
| 1244 | |
| 1245 | start_idx = (int)tv_get_number_chk(&argvars[2], &error); |
| 1246 | if (error || start_idx >= (int)STRLEN(haystack)) |
| 1247 | return; |
| 1248 | if (start_idx >= 0) |
| 1249 | haystack += start_idx; |
| 1250 | } |
| 1251 | |
| 1252 | pos = (char_u *)strstr((char *)haystack, (char *)needle); |
| 1253 | if (pos != NULL) |
| 1254 | rettv->vval.v_number = (varnumber_T)(pos - save_haystack); |
| 1255 | } |
| 1256 | |
| 1257 | /* |
| 1258 | * "string()" function |
| 1259 | */ |
| 1260 | void |
| 1261 | f_string(typval_T *argvars, typval_T *rettv) |
| 1262 | { |
| 1263 | char_u *tofree; |
| 1264 | char_u numbuf[NUMBUFLEN]; |
| 1265 | |
| 1266 | rettv->v_type = VAR_STRING; |
| 1267 | rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, |
| 1268 | get_copyID()); |
| 1269 | // Make a copy if we have a value but it's not in allocated memory. |
| 1270 | if (rettv->vval.v_string != NULL && tofree == NULL) |
| 1271 | rettv->vval.v_string = vim_strsave(rettv->vval.v_string); |
| 1272 | } |
| 1273 | |
| 1274 | /* |
| 1275 | * "strlen()" function |
| 1276 | */ |
| 1277 | void |
| 1278 | f_strlen(typval_T *argvars, typval_T *rettv) |
| 1279 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1280 | if (in_vim9script() |
| 1281 | && check_for_string_or_number_arg(argvars, 0) == FAIL) |
| 1282 | return; |
| 1283 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1284 | rettv->vval.v_number = (varnumber_T)(STRLEN( |
| 1285 | tv_get_string(&argvars[0]))); |
| 1286 | } |
| 1287 | |
| 1288 | static void |
| 1289 | strchar_common(typval_T *argvars, typval_T *rettv, int skipcc) |
| 1290 | { |
| 1291 | char_u *s = tv_get_string(&argvars[0]); |
| 1292 | varnumber_T len = 0; |
| 1293 | int (*func_mb_ptr2char_adv)(char_u **pp); |
| 1294 | |
| 1295 | func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv; |
| 1296 | while (*s != NUL) |
| 1297 | { |
| 1298 | func_mb_ptr2char_adv(&s); |
| 1299 | ++len; |
| 1300 | } |
| 1301 | rettv->vval.v_number = len; |
| 1302 | } |
| 1303 | |
| 1304 | /* |
| 1305 | * "strcharlen()" function |
| 1306 | */ |
| 1307 | void |
| 1308 | f_strcharlen(typval_T *argvars, typval_T *rettv) |
| 1309 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1310 | if (in_vim9script() |
| 1311 | && check_for_string_or_number_arg(argvars, 0) == FAIL) |
| 1312 | return; |
| 1313 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1314 | strchar_common(argvars, rettv, TRUE); |
| 1315 | } |
| 1316 | |
| 1317 | /* |
| 1318 | * "strchars()" function |
| 1319 | */ |
| 1320 | void |
| 1321 | f_strchars(typval_T *argvars, typval_T *rettv) |
| 1322 | { |
| 1323 | varnumber_T skipcc = FALSE; |
| 1324 | |
Yegappan Lakshmanan | a9a7c0c | 2021-07-17 19:11:07 +0200 | [diff] [blame] | 1325 | if (in_vim9script() |
| 1326 | && (check_for_string_arg(argvars, 0) == FAIL |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1327 | || check_for_opt_bool_arg(argvars, 1) == FAIL)) |
Yegappan Lakshmanan | a9a7c0c | 2021-07-17 19:11:07 +0200 | [diff] [blame] | 1328 | return; |
| 1329 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1330 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1331 | skipcc = tv_get_bool(&argvars[1]); |
| 1332 | if (skipcc < 0 || skipcc > 1) |
| 1333 | semsg(_(e_using_number_as_bool_nr), skipcc); |
| 1334 | else |
| 1335 | strchar_common(argvars, rettv, skipcc); |
| 1336 | } |
| 1337 | |
| 1338 | /* |
| 1339 | * "strdisplaywidth()" function |
| 1340 | */ |
| 1341 | void |
| 1342 | f_strdisplaywidth(typval_T *argvars, typval_T *rettv) |
| 1343 | { |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 1344 | char_u *s; |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1345 | int col = 0; |
| 1346 | |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 1347 | rettv->vval.v_number = -1; |
| 1348 | |
| 1349 | if (in_vim9script() |
| 1350 | && (check_for_string_arg(argvars, 0) == FAIL |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1351 | || check_for_opt_number_arg(argvars, 1) == FAIL)) |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 1352 | return; |
| 1353 | |
| 1354 | s = tv_get_string(&argvars[0]); |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1355 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 1356 | col = (int)tv_get_number(&argvars[1]); |
| 1357 | |
| 1358 | rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col); |
| 1359 | } |
| 1360 | |
| 1361 | /* |
| 1362 | * "strwidth()" function |
| 1363 | */ |
| 1364 | void |
| 1365 | f_strwidth(typval_T *argvars, typval_T *rettv) |
| 1366 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1367 | char_u *s; |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1368 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1369 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
| 1370 | return; |
| 1371 | |
| 1372 | s = tv_get_string_strict(&argvars[0]); |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1373 | rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1)); |
| 1374 | } |
| 1375 | |
| 1376 | /* |
| 1377 | * "strcharpart()" function |
| 1378 | */ |
| 1379 | void |
| 1380 | f_strcharpart(typval_T *argvars, typval_T *rettv) |
| 1381 | { |
| 1382 | char_u *p; |
| 1383 | int nchar; |
| 1384 | int nbyte = 0; |
| 1385 | int charlen; |
| 1386 | int skipcc = FALSE; |
| 1387 | int len = 0; |
| 1388 | int slen; |
| 1389 | int error = FALSE; |
| 1390 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1391 | if (in_vim9script() |
| 1392 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1393 | || check_for_number_arg(argvars, 1) == FAIL |
| 1394 | || check_for_opt_number_arg(argvars, 2) == FAIL |
| 1395 | || (argvars[2].v_type != VAR_UNKNOWN |
| 1396 | && check_for_opt_bool_arg(argvars, 3) == FAIL))) |
| 1397 | return; |
| 1398 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1399 | p = tv_get_string(&argvars[0]); |
| 1400 | slen = (int)STRLEN(p); |
| 1401 | |
| 1402 | nchar = (int)tv_get_number_chk(&argvars[1], &error); |
| 1403 | if (!error) |
| 1404 | { |
| 1405 | if (argvars[2].v_type != VAR_UNKNOWN |
| 1406 | && argvars[3].v_type != VAR_UNKNOWN) |
| 1407 | { |
| 1408 | skipcc = tv_get_bool(&argvars[3]); |
| 1409 | if (skipcc < 0 || skipcc > 1) |
| 1410 | { |
| 1411 | semsg(_(e_using_number_as_bool_nr), skipcc); |
| 1412 | return; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | if (nchar > 0) |
| 1417 | while (nchar > 0 && nbyte < slen) |
| 1418 | { |
| 1419 | if (skipcc) |
| 1420 | nbyte += mb_ptr2len(p + nbyte); |
| 1421 | else |
| 1422 | nbyte += MB_CPTR2LEN(p + nbyte); |
| 1423 | --nchar; |
| 1424 | } |
| 1425 | else |
| 1426 | nbyte = nchar; |
| 1427 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1428 | { |
| 1429 | charlen = (int)tv_get_number(&argvars[2]); |
| 1430 | while (charlen > 0 && nbyte + len < slen) |
| 1431 | { |
| 1432 | int off = nbyte + len; |
| 1433 | |
| 1434 | if (off < 0) |
| 1435 | len += 1; |
| 1436 | else |
| 1437 | { |
| 1438 | if (skipcc) |
| 1439 | len += mb_ptr2len(p + off); |
| 1440 | else |
| 1441 | len += MB_CPTR2LEN(p + off); |
| 1442 | } |
| 1443 | --charlen; |
| 1444 | } |
| 1445 | } |
| 1446 | else |
| 1447 | len = slen - nbyte; // default: all bytes that are available. |
| 1448 | } |
| 1449 | |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 1450 | // Only return the overlap between the specified part and the actual |
| 1451 | // string. |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1452 | if (nbyte < 0) |
| 1453 | { |
| 1454 | len += nbyte; |
| 1455 | nbyte = 0; |
| 1456 | } |
| 1457 | else if (nbyte > slen) |
| 1458 | nbyte = slen; |
| 1459 | if (len < 0) |
| 1460 | len = 0; |
| 1461 | else if (nbyte + len > slen) |
| 1462 | len = slen - nbyte; |
| 1463 | |
| 1464 | rettv->v_type = VAR_STRING; |
| 1465 | rettv->vval.v_string = vim_strnsave(p + nbyte, len); |
| 1466 | } |
| 1467 | |
| 1468 | /* |
| 1469 | * "strpart()" function |
| 1470 | */ |
| 1471 | void |
| 1472 | f_strpart(typval_T *argvars, typval_T *rettv) |
| 1473 | { |
| 1474 | char_u *p; |
| 1475 | int n; |
| 1476 | int len; |
| 1477 | int slen; |
| 1478 | int error = FALSE; |
| 1479 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1480 | if (in_vim9script() |
| 1481 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1482 | || check_for_number_arg(argvars, 1) == FAIL |
| 1483 | || check_for_opt_number_arg(argvars, 2) == FAIL |
| 1484 | || (argvars[2].v_type != VAR_UNKNOWN |
| 1485 | && check_for_opt_bool_arg(argvars, 3) == FAIL))) |
| 1486 | return; |
| 1487 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1488 | p = tv_get_string(&argvars[0]); |
| 1489 | slen = (int)STRLEN(p); |
| 1490 | |
| 1491 | n = (int)tv_get_number_chk(&argvars[1], &error); |
| 1492 | if (error) |
| 1493 | len = 0; |
| 1494 | else if (argvars[2].v_type != VAR_UNKNOWN) |
| 1495 | len = (int)tv_get_number(&argvars[2]); |
| 1496 | else |
| 1497 | len = slen - n; // default len: all bytes that are available. |
| 1498 | |
| 1499 | // Only return the overlap between the specified part and the actual |
| 1500 | // string. |
| 1501 | if (n < 0) |
| 1502 | { |
| 1503 | len += n; |
| 1504 | n = 0; |
| 1505 | } |
| 1506 | else if (n > slen) |
| 1507 | n = slen; |
| 1508 | if (len < 0) |
| 1509 | len = 0; |
| 1510 | else if (n + len > slen) |
| 1511 | len = slen - n; |
| 1512 | |
| 1513 | if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN) |
| 1514 | { |
| 1515 | int off; |
| 1516 | |
| 1517 | // length in characters |
| 1518 | for (off = n; off < slen && len > 0; --len) |
| 1519 | off += mb_ptr2len(p + off); |
| 1520 | len = off - n; |
| 1521 | } |
| 1522 | |
| 1523 | rettv->v_type = VAR_STRING; |
| 1524 | rettv->vval.v_string = vim_strnsave(p + n, len); |
| 1525 | } |
| 1526 | |
| 1527 | /* |
| 1528 | * "strridx()" function |
| 1529 | */ |
| 1530 | void |
| 1531 | f_strridx(typval_T *argvars, typval_T *rettv) |
| 1532 | { |
| 1533 | char_u buf[NUMBUFLEN]; |
| 1534 | char_u *needle; |
| 1535 | char_u *haystack; |
| 1536 | char_u *rest; |
| 1537 | char_u *lastmatch = NULL; |
| 1538 | int haystack_len, end_idx; |
| 1539 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1540 | if (in_vim9script() |
| 1541 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1542 | || check_for_string_arg(argvars, 1) == FAIL |
| 1543 | || check_for_opt_number_arg(argvars, 2) == FAIL)) |
| 1544 | return; |
| 1545 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1546 | needle = tv_get_string_chk(&argvars[1]); |
| 1547 | haystack = tv_get_string_buf_chk(&argvars[0], buf); |
| 1548 | |
| 1549 | rettv->vval.v_number = -1; |
| 1550 | if (needle == NULL || haystack == NULL) |
| 1551 | return; // type error; errmsg already given |
| 1552 | |
| 1553 | haystack_len = (int)STRLEN(haystack); |
| 1554 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1555 | { |
| 1556 | // Third argument: upper limit for index |
| 1557 | end_idx = (int)tv_get_number_chk(&argvars[2], NULL); |
| 1558 | if (end_idx < 0) |
| 1559 | return; // can never find a match |
| 1560 | } |
| 1561 | else |
| 1562 | end_idx = haystack_len; |
| 1563 | |
| 1564 | if (*needle == NUL) |
| 1565 | { |
| 1566 | // Empty string matches past the end. |
| 1567 | lastmatch = haystack + end_idx; |
| 1568 | } |
| 1569 | else |
| 1570 | { |
| 1571 | for (rest = haystack; *rest != '\0'; ++rest) |
| 1572 | { |
| 1573 | rest = (char_u *)strstr((char *)rest, (char *)needle); |
| 1574 | if (rest == NULL || rest > haystack + end_idx) |
| 1575 | break; |
| 1576 | lastmatch = rest; |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | if (lastmatch == NULL) |
| 1581 | rettv->vval.v_number = -1; |
| 1582 | else |
| 1583 | rettv->vval.v_number = (varnumber_T)(lastmatch - haystack); |
| 1584 | } |
| 1585 | |
| 1586 | /* |
| 1587 | * "strtrans()" function |
| 1588 | */ |
| 1589 | void |
| 1590 | f_strtrans(typval_T *argvars, typval_T *rettv) |
| 1591 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1592 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
| 1593 | return; |
| 1594 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1595 | rettv->v_type = VAR_STRING; |
| 1596 | rettv->vval.v_string = transstr(tv_get_string(&argvars[0])); |
| 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * "tolower(string)" function |
| 1601 | */ |
| 1602 | void |
| 1603 | f_tolower(typval_T *argvars, typval_T *rettv) |
| 1604 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1605 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
| 1606 | return; |
| 1607 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1608 | rettv->v_type = VAR_STRING; |
| 1609 | rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0])); |
| 1610 | } |
| 1611 | |
| 1612 | /* |
| 1613 | * "toupper(string)" function |
| 1614 | */ |
| 1615 | void |
| 1616 | f_toupper(typval_T *argvars, typval_T *rettv) |
| 1617 | { |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1618 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
| 1619 | return; |
| 1620 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1621 | rettv->v_type = VAR_STRING; |
| 1622 | rettv->vval.v_string = strup_save(tv_get_string(&argvars[0])); |
| 1623 | } |
| 1624 | |
| 1625 | /* |
| 1626 | * "tr(string, fromstr, tostr)" function |
| 1627 | */ |
| 1628 | void |
| 1629 | f_tr(typval_T *argvars, typval_T *rettv) |
| 1630 | { |
| 1631 | char_u *in_str; |
| 1632 | char_u *fromstr; |
| 1633 | char_u *tostr; |
| 1634 | char_u *p; |
| 1635 | int inlen; |
| 1636 | int fromlen; |
| 1637 | int tolen; |
| 1638 | int idx; |
| 1639 | char_u *cpstr; |
| 1640 | int cplen; |
| 1641 | int first = TRUE; |
| 1642 | char_u buf[NUMBUFLEN]; |
| 1643 | char_u buf2[NUMBUFLEN]; |
| 1644 | garray_T ga; |
| 1645 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1646 | if (in_vim9script() |
| 1647 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1648 | || check_for_string_arg(argvars, 1) == FAIL |
| 1649 | || check_for_string_arg(argvars, 2) == FAIL)) |
| 1650 | return; |
| 1651 | |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1652 | in_str = tv_get_string(&argvars[0]); |
| 1653 | fromstr = tv_get_string_buf_chk(&argvars[1], buf); |
| 1654 | tostr = tv_get_string_buf_chk(&argvars[2], buf2); |
| 1655 | |
| 1656 | // Default return value: empty string. |
| 1657 | rettv->v_type = VAR_STRING; |
| 1658 | rettv->vval.v_string = NULL; |
| 1659 | if (fromstr == NULL || tostr == NULL) |
| 1660 | return; // type error; errmsg already given |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 1661 | ga_init2(&ga, sizeof(char), 80); |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1662 | |
| 1663 | if (!has_mbyte) |
| 1664 | // not multi-byte: fromstr and tostr must be the same length |
| 1665 | if (STRLEN(fromstr) != STRLEN(tostr)) |
| 1666 | { |
| 1667 | error: |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1668 | semsg(_(e_invalid_argument_str), fromstr); |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1669 | ga_clear(&ga); |
| 1670 | return; |
| 1671 | } |
| 1672 | |
| 1673 | // fromstr and tostr have to contain the same number of chars |
| 1674 | while (*in_str != NUL) |
| 1675 | { |
| 1676 | if (has_mbyte) |
| 1677 | { |
| 1678 | inlen = (*mb_ptr2len)(in_str); |
| 1679 | cpstr = in_str; |
| 1680 | cplen = inlen; |
| 1681 | idx = 0; |
| 1682 | for (p = fromstr; *p != NUL; p += fromlen) |
| 1683 | { |
| 1684 | fromlen = (*mb_ptr2len)(p); |
| 1685 | if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0) |
| 1686 | { |
| 1687 | for (p = tostr; *p != NUL; p += tolen) |
| 1688 | { |
| 1689 | tolen = (*mb_ptr2len)(p); |
| 1690 | if (idx-- == 0) |
| 1691 | { |
| 1692 | cplen = tolen; |
| 1693 | cpstr = p; |
| 1694 | break; |
| 1695 | } |
| 1696 | } |
| 1697 | if (*p == NUL) // tostr is shorter than fromstr |
| 1698 | goto error; |
| 1699 | break; |
| 1700 | } |
| 1701 | ++idx; |
| 1702 | } |
| 1703 | |
| 1704 | if (first && cpstr == in_str) |
| 1705 | { |
| 1706 | // Check that fromstr and tostr have the same number of |
| 1707 | // (multi-byte) characters. Done only once when a character |
| 1708 | // of in_str doesn't appear in fromstr. |
| 1709 | first = FALSE; |
| 1710 | for (p = tostr; *p != NUL; p += tolen) |
| 1711 | { |
| 1712 | tolen = (*mb_ptr2len)(p); |
| 1713 | --idx; |
| 1714 | } |
| 1715 | if (idx != 0) |
| 1716 | goto error; |
| 1717 | } |
| 1718 | |
| 1719 | (void)ga_grow(&ga, cplen); |
| 1720 | mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen); |
| 1721 | ga.ga_len += cplen; |
| 1722 | |
| 1723 | in_str += inlen; |
| 1724 | } |
| 1725 | else |
| 1726 | { |
| 1727 | // When not using multi-byte chars we can do it faster. |
| 1728 | p = vim_strchr(fromstr, *in_str); |
| 1729 | if (p != NULL) |
| 1730 | ga_append(&ga, tostr[p - fromstr]); |
| 1731 | else |
| 1732 | ga_append(&ga, *in_str); |
| 1733 | ++in_str; |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | // add a terminating NUL |
| 1738 | (void)ga_grow(&ga, 1); |
| 1739 | ga_append(&ga, NUL); |
| 1740 | |
| 1741 | rettv->vval.v_string = ga.ga_data; |
| 1742 | } |
| 1743 | |
| 1744 | /* |
| 1745 | * "trim({expr})" function |
| 1746 | */ |
| 1747 | void |
| 1748 | f_trim(typval_T *argvars, typval_T *rettv) |
| 1749 | { |
| 1750 | char_u buf1[NUMBUFLEN]; |
| 1751 | char_u buf2[NUMBUFLEN]; |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1752 | char_u *head; |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1753 | char_u *mask = NULL; |
| 1754 | char_u *tail; |
| 1755 | char_u *prev; |
| 1756 | char_u *p; |
| 1757 | int c1; |
| 1758 | int dir = 0; |
| 1759 | |
| 1760 | rettv->v_type = VAR_STRING; |
| 1761 | rettv->vval.v_string = NULL; |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 1762 | |
| 1763 | if (in_vim9script() |
| 1764 | && (check_for_string_arg(argvars, 0) == FAIL |
| 1765 | || check_for_opt_string_arg(argvars, 1) == FAIL |
| 1766 | || (argvars[1].v_type != VAR_UNKNOWN |
| 1767 | && check_for_opt_number_arg(argvars, 2) == FAIL))) |
| 1768 | return; |
| 1769 | |
| 1770 | head = tv_get_string_buf_chk(&argvars[0], buf1); |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1771 | if (head == NULL) |
| 1772 | return; |
| 1773 | |
Yegappan Lakshmanan | 8deb2b3 | 2022-09-02 15:15:27 +0100 | [diff] [blame] | 1774 | if (check_for_opt_string_arg(argvars, 1) == FAIL) |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1775 | return; |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1776 | |
| 1777 | if (argvars[1].v_type == VAR_STRING) |
| 1778 | { |
| 1779 | mask = tv_get_string_buf_chk(&argvars[1], buf2); |
| 1780 | |
| 1781 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1782 | { |
| 1783 | int error = 0; |
| 1784 | |
| 1785 | // leading or trailing characters to trim |
| 1786 | dir = (int)tv_get_number_chk(&argvars[2], &error); |
| 1787 | if (error) |
| 1788 | return; |
| 1789 | if (dir < 0 || dir > 2) |
| 1790 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1791 | semsg(_(e_invalid_argument_str), tv_get_string(&argvars[2])); |
Yegappan Lakshmanan | a243813 | 2021-07-10 21:29:18 +0200 | [diff] [blame] | 1792 | return; |
| 1793 | } |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | if (dir == 0 || dir == 1) |
| 1798 | { |
| 1799 | // Trim leading characters |
| 1800 | while (*head != NUL) |
| 1801 | { |
| 1802 | c1 = PTR2CHAR(head); |
| 1803 | if (mask == NULL) |
| 1804 | { |
| 1805 | if (c1 > ' ' && c1 != 0xa0) |
| 1806 | break; |
| 1807 | } |
| 1808 | else |
| 1809 | { |
| 1810 | for (p = mask; *p != NUL; MB_PTR_ADV(p)) |
| 1811 | if (c1 == PTR2CHAR(p)) |
| 1812 | break; |
| 1813 | if (*p == NUL) |
| 1814 | break; |
| 1815 | } |
| 1816 | MB_PTR_ADV(head); |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | tail = head + STRLEN(head); |
| 1821 | if (dir == 0 || dir == 2) |
| 1822 | { |
| 1823 | // Trim trailing characters |
| 1824 | for (; tail > head; tail = prev) |
| 1825 | { |
| 1826 | prev = tail; |
| 1827 | MB_PTR_BACK(head, prev); |
| 1828 | c1 = PTR2CHAR(prev); |
| 1829 | if (mask == NULL) |
| 1830 | { |
| 1831 | if (c1 > ' ' && c1 != 0xa0) |
| 1832 | break; |
| 1833 | } |
| 1834 | else |
| 1835 | { |
| 1836 | for (p = mask; *p != NUL; MB_PTR_ADV(p)) |
| 1837 | if (c1 == PTR2CHAR(p)) |
| 1838 | break; |
| 1839 | if (*p == NUL) |
| 1840 | break; |
| 1841 | } |
| 1842 | } |
| 1843 | } |
| 1844 | rettv->vval.v_string = vim_strnsave(head, tail - head); |
| 1845 | } |
| 1846 | |
Bram Moolenaar | 677658a | 2022-01-05 16:09:06 +0000 | [diff] [blame] | 1847 | static char *e_printf = N_(e_insufficient_arguments_for_printf); |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 1848 | |
| 1849 | /* |
| 1850 | * Get number argument from "idxp" entry in "tvs". First entry is 1. |
| 1851 | */ |
| 1852 | static varnumber_T |
| 1853 | tv_nr(typval_T *tvs, int *idxp) |
| 1854 | { |
| 1855 | int idx = *idxp - 1; |
| 1856 | varnumber_T n = 0; |
| 1857 | int err = FALSE; |
| 1858 | |
| 1859 | if (tvs[idx].v_type == VAR_UNKNOWN) |
| 1860 | emsg(_(e_printf)); |
| 1861 | else |
| 1862 | { |
| 1863 | ++*idxp; |
| 1864 | n = tv_get_number_chk(&tvs[idx], &err); |
| 1865 | if (err) |
| 1866 | n = 0; |
| 1867 | } |
| 1868 | return n; |
| 1869 | } |
| 1870 | |
| 1871 | /* |
| 1872 | * Get string argument from "idxp" entry in "tvs". First entry is 1. |
| 1873 | * If "tofree" is NULL tv_get_string_chk() is used. Some types (e.g. List) |
| 1874 | * are not converted to a string. |
| 1875 | * If "tofree" is not NULL echo_string() is used. All types are converted to |
| 1876 | * a string with the same format as ":echo". The caller must free "*tofree". |
| 1877 | * Returns NULL for an error. |
| 1878 | */ |
| 1879 | static char * |
| 1880 | tv_str(typval_T *tvs, int *idxp, char_u **tofree) |
| 1881 | { |
| 1882 | int idx = *idxp - 1; |
| 1883 | char *s = NULL; |
| 1884 | static char_u numbuf[NUMBUFLEN]; |
| 1885 | |
| 1886 | if (tvs[idx].v_type == VAR_UNKNOWN) |
| 1887 | emsg(_(e_printf)); |
| 1888 | else |
| 1889 | { |
| 1890 | ++*idxp; |
| 1891 | if (tofree != NULL) |
| 1892 | s = (char *)echo_string(&tvs[idx], tofree, numbuf, get_copyID()); |
| 1893 | else |
| 1894 | s = (char *)tv_get_string_chk(&tvs[idx]); |
| 1895 | } |
| 1896 | return s; |
| 1897 | } |
| 1898 | |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 1899 | /* |
| 1900 | * Get float argument from "idxp" entry in "tvs". First entry is 1. |
| 1901 | */ |
| 1902 | static double |
| 1903 | tv_float(typval_T *tvs, int *idxp) |
| 1904 | { |
| 1905 | int idx = *idxp - 1; |
| 1906 | double f = 0; |
| 1907 | |
| 1908 | if (tvs[idx].v_type == VAR_UNKNOWN) |
| 1909 | emsg(_(e_printf)); |
| 1910 | else |
| 1911 | { |
| 1912 | ++*idxp; |
| 1913 | if (tvs[idx].v_type == VAR_FLOAT) |
| 1914 | f = tvs[idx].vval.v_float; |
| 1915 | else if (tvs[idx].v_type == VAR_NUMBER) |
| 1916 | f = (double)tvs[idx].vval.v_number; |
| 1917 | else |
Bram Moolenaar | 9d00e4a | 2022-01-05 17:49:15 +0000 | [diff] [blame] | 1918 | emsg(_(e_expected_float_argument_for_printf)); |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 1919 | } |
| 1920 | return f; |
| 1921 | } |
Yegappan Lakshmanan | f973eeb | 2021-12-22 18:19:26 +0000 | [diff] [blame] | 1922 | |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 1923 | #endif |
| 1924 | |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 1925 | /* |
| 1926 | * Return the representation of infinity for printf() function: |
| 1927 | * "-inf", "inf", "+inf", " inf", "-INF", "INF", "+INF" or " INF". |
| 1928 | */ |
| 1929 | static const char * |
| 1930 | infinity_str(int positive, |
| 1931 | char fmt_spec, |
| 1932 | int force_sign, |
| 1933 | int space_for_positive) |
| 1934 | { |
| 1935 | static const char *table[] = |
| 1936 | { |
| 1937 | "-inf", "inf", "+inf", " inf", |
| 1938 | "-INF", "INF", "+INF", " INF" |
| 1939 | }; |
| 1940 | int idx = positive * (1 + force_sign + force_sign * space_for_positive); |
| 1941 | |
| 1942 | if (ASCII_ISUPPER(fmt_spec)) |
| 1943 | idx += 4; |
| 1944 | return table[idx]; |
| 1945 | } |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 1946 | |
| 1947 | /* |
| 1948 | * This code was included to provide a portable vsnprintf() and snprintf(). |
| 1949 | * Some systems may provide their own, but we always use this one for |
| 1950 | * consistency. |
| 1951 | * |
| 1952 | * This code is based on snprintf.c - a portable implementation of snprintf |
| 1953 | * by Mark Martinec <mark.martinec@ijs.si>, Version 2.2, 2000-10-06. |
| 1954 | * Included with permission. It was heavily modified to fit in Vim. |
| 1955 | * The original code, including useful comments, can be found here: |
| 1956 | * http://www.ijs.si/software/snprintf/ |
| 1957 | * |
| 1958 | * This snprintf() only supports the following conversion specifiers: |
| 1959 | * s, c, d, u, o, x, X, p (and synonyms: i, D, U, O - see below) |
| 1960 | * with flags: '-', '+', ' ', '0' and '#'. |
| 1961 | * An asterisk is supported for field width as well as precision. |
| 1962 | * |
| 1963 | * Limited support for floating point was added: 'f', 'F', 'e', 'E', 'g', 'G'. |
| 1964 | * |
| 1965 | * Length modifiers 'h' (short int) and 'l' (long int) and 'll' (long long int) |
| 1966 | * are supported. NOTE: for 'll' the argument is varnumber_T or uvarnumber_T. |
| 1967 | * |
| 1968 | * The locale is not used, the string is used as a byte string. This is only |
| 1969 | * relevant for double-byte encodings where the second byte may be '%'. |
| 1970 | * |
| 1971 | * It is permitted for "str_m" to be zero, and it is permitted to specify NULL |
| 1972 | * pointer for resulting string argument if "str_m" is zero (as per ISO C99). |
| 1973 | * |
| 1974 | * The return value is the number of characters which would be generated |
| 1975 | * for the given input, excluding the trailing NUL. If this value |
| 1976 | * is greater or equal to "str_m", not all characters from the result |
| 1977 | * have been stored in str, output bytes beyond the ("str_m"-1) -th character |
| 1978 | * are discarded. If "str_m" is greater than zero it is guaranteed |
| 1979 | * the resulting string will be NUL-terminated. |
| 1980 | */ |
| 1981 | |
| 1982 | /* |
| 1983 | * When va_list is not supported we only define vim_snprintf(). |
| 1984 | * |
| 1985 | * vim_vsnprintf_typval() can be invoked with either "va_list" or a list of |
| 1986 | * "typval_T". When the latter is not used it must be NULL. |
| 1987 | */ |
| 1988 | |
| 1989 | // When generating prototypes all of this is skipped, cproto doesn't |
| 1990 | // understand this. |
| 1991 | #ifndef PROTO |
| 1992 | |
| 1993 | // Like vim_vsnprintf() but append to the string. |
| 1994 | int |
| 1995 | vim_snprintf_add(char *str, size_t str_m, const char *fmt, ...) |
| 1996 | { |
| 1997 | va_list ap; |
| 1998 | int str_l; |
| 1999 | size_t len = STRLEN(str); |
| 2000 | size_t space; |
| 2001 | |
| 2002 | if (str_m <= len) |
| 2003 | space = 0; |
| 2004 | else |
| 2005 | space = str_m - len; |
| 2006 | va_start(ap, fmt); |
| 2007 | str_l = vim_vsnprintf(str + len, space, fmt, ap); |
| 2008 | va_end(ap); |
| 2009 | return str_l; |
| 2010 | } |
| 2011 | |
| 2012 | int |
| 2013 | vim_snprintf(char *str, size_t str_m, const char *fmt, ...) |
| 2014 | { |
| 2015 | va_list ap; |
| 2016 | int str_l; |
| 2017 | |
| 2018 | va_start(ap, fmt); |
| 2019 | str_l = vim_vsnprintf(str, str_m, fmt, ap); |
| 2020 | va_end(ap); |
| 2021 | return str_l; |
| 2022 | } |
| 2023 | |
| 2024 | int |
| 2025 | vim_vsnprintf( |
| 2026 | char *str, |
| 2027 | size_t str_m, |
| 2028 | const char *fmt, |
| 2029 | va_list ap) |
| 2030 | { |
| 2031 | return vim_vsnprintf_typval(str, str_m, fmt, ap, NULL); |
| 2032 | } |
| 2033 | |
| 2034 | int |
| 2035 | vim_vsnprintf_typval( |
| 2036 | char *str, |
| 2037 | size_t str_m, |
| 2038 | const char *fmt, |
| 2039 | va_list ap, |
| 2040 | typval_T *tvs) |
| 2041 | { |
| 2042 | size_t str_l = 0; |
| 2043 | const char *p = fmt; |
| 2044 | int arg_idx = 1; |
| 2045 | |
| 2046 | if (p == NULL) |
| 2047 | p = ""; |
| 2048 | while (*p != NUL) |
| 2049 | { |
| 2050 | if (*p != '%') |
| 2051 | { |
| 2052 | char *q = strchr(p + 1, '%'); |
| 2053 | size_t n = (q == NULL) ? STRLEN(p) : (size_t)(q - p); |
| 2054 | |
| 2055 | // Copy up to the next '%' or NUL without any changes. |
| 2056 | if (str_l < str_m) |
| 2057 | { |
| 2058 | size_t avail = str_m - str_l; |
| 2059 | |
| 2060 | mch_memmove(str + str_l, p, n > avail ? avail : n); |
| 2061 | } |
| 2062 | p += n; |
| 2063 | str_l += n; |
| 2064 | } |
| 2065 | else |
| 2066 | { |
| 2067 | size_t min_field_width = 0, precision = 0; |
| 2068 | int zero_padding = 0, precision_specified = 0, justify_left = 0; |
| 2069 | int alternate_form = 0, force_sign = 0; |
| 2070 | |
| 2071 | // If both the ' ' and '+' flags appear, the ' ' flag should be |
| 2072 | // ignored. |
| 2073 | int space_for_positive = 1; |
| 2074 | |
| 2075 | // allowed values: \0, h, l, L |
| 2076 | char length_modifier = '\0'; |
| 2077 | |
| 2078 | // temporary buffer for simple numeric->string conversion |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 2079 | # define TMP_LEN 350 // On my system 1e308 is the biggest number possible. |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2080 | // That sounds reasonable to use as the maximum |
| 2081 | // printable. |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2082 | char tmp[TMP_LEN]; |
| 2083 | |
| 2084 | // string address in case of string argument |
| 2085 | const char *str_arg = NULL; |
| 2086 | |
| 2087 | // natural field width of arg without padding and sign |
| 2088 | size_t str_arg_l; |
| 2089 | |
| 2090 | // unsigned char argument value - only defined for c conversion. |
| 2091 | // N.B. standard explicitly states the char argument for the c |
| 2092 | // conversion is unsigned |
| 2093 | unsigned char uchar_arg; |
| 2094 | |
| 2095 | // number of zeros to be inserted for numeric conversions as |
| 2096 | // required by the precision or minimal field width |
| 2097 | size_t number_of_zeros_to_pad = 0; |
| 2098 | |
| 2099 | // index into tmp where zero padding is to be inserted |
| 2100 | size_t zero_padding_insertion_ind = 0; |
| 2101 | |
| 2102 | // current conversion specifier character |
| 2103 | char fmt_spec = '\0'; |
| 2104 | |
| 2105 | // buffer for 's' and 'S' specs |
| 2106 | char_u *tofree = NULL; |
| 2107 | |
| 2108 | |
| 2109 | p++; // skip '%' |
| 2110 | |
| 2111 | // parse flags |
| 2112 | while (*p == '0' || *p == '-' || *p == '+' || *p == ' ' |
| 2113 | || *p == '#' || *p == '\'') |
| 2114 | { |
| 2115 | switch (*p) |
| 2116 | { |
| 2117 | case '0': zero_padding = 1; break; |
| 2118 | case '-': justify_left = 1; break; |
| 2119 | case '+': force_sign = 1; space_for_positive = 0; break; |
| 2120 | case ' ': force_sign = 1; |
| 2121 | // If both the ' ' and '+' flags appear, the ' ' |
| 2122 | // flag should be ignored |
| 2123 | break; |
| 2124 | case '#': alternate_form = 1; break; |
| 2125 | case '\'': break; |
| 2126 | } |
| 2127 | p++; |
| 2128 | } |
| 2129 | // If the '0' and '-' flags both appear, the '0' flag should be |
| 2130 | // ignored. |
| 2131 | |
| 2132 | // parse field width |
| 2133 | if (*p == '*') |
| 2134 | { |
| 2135 | int j; |
| 2136 | |
| 2137 | p++; |
| 2138 | j = |
| 2139 | # if defined(FEAT_EVAL) |
| 2140 | tvs != NULL ? tv_nr(tvs, &arg_idx) : |
| 2141 | # endif |
| 2142 | va_arg(ap, int); |
| 2143 | if (j >= 0) |
| 2144 | min_field_width = j; |
| 2145 | else |
| 2146 | { |
| 2147 | min_field_width = -j; |
| 2148 | justify_left = 1; |
| 2149 | } |
| 2150 | } |
| 2151 | else if (VIM_ISDIGIT((int)(*p))) |
| 2152 | { |
| 2153 | // size_t could be wider than unsigned int; make sure we treat |
| 2154 | // argument like common implementations do |
| 2155 | unsigned int uj = *p++ - '0'; |
| 2156 | |
| 2157 | while (VIM_ISDIGIT((int)(*p))) |
| 2158 | uj = 10 * uj + (unsigned int)(*p++ - '0'); |
| 2159 | min_field_width = uj; |
| 2160 | } |
| 2161 | |
| 2162 | // parse precision |
| 2163 | if (*p == '.') |
| 2164 | { |
| 2165 | p++; |
| 2166 | precision_specified = 1; |
| 2167 | if (*p == '*') |
| 2168 | { |
| 2169 | int j; |
| 2170 | |
| 2171 | j = |
| 2172 | # if defined(FEAT_EVAL) |
| 2173 | tvs != NULL ? tv_nr(tvs, &arg_idx) : |
| 2174 | # endif |
| 2175 | va_arg(ap, int); |
| 2176 | p++; |
| 2177 | if (j >= 0) |
| 2178 | precision = j; |
| 2179 | else |
| 2180 | { |
| 2181 | precision_specified = 0; |
| 2182 | precision = 0; |
| 2183 | } |
| 2184 | } |
| 2185 | else if (VIM_ISDIGIT((int)(*p))) |
| 2186 | { |
| 2187 | // size_t could be wider than unsigned int; make sure we |
| 2188 | // treat argument like common implementations do |
| 2189 | unsigned int uj = *p++ - '0'; |
| 2190 | |
| 2191 | while (VIM_ISDIGIT((int)(*p))) |
| 2192 | uj = 10 * uj + (unsigned int)(*p++ - '0'); |
| 2193 | precision = uj; |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | // parse 'h', 'l' and 'll' length modifiers |
| 2198 | if (*p == 'h' || *p == 'l') |
| 2199 | { |
| 2200 | length_modifier = *p; |
| 2201 | p++; |
| 2202 | if (length_modifier == 'l' && *p == 'l') |
| 2203 | { |
| 2204 | // double l = __int64 / varnumber_T |
| 2205 | length_modifier = 'L'; |
| 2206 | p++; |
| 2207 | } |
| 2208 | } |
| 2209 | fmt_spec = *p; |
| 2210 | |
| 2211 | // common synonyms: |
| 2212 | switch (fmt_spec) |
| 2213 | { |
| 2214 | case 'i': fmt_spec = 'd'; break; |
| 2215 | case 'D': fmt_spec = 'd'; length_modifier = 'l'; break; |
| 2216 | case 'U': fmt_spec = 'u'; length_modifier = 'l'; break; |
| 2217 | case 'O': fmt_spec = 'o'; length_modifier = 'l'; break; |
| 2218 | default: break; |
| 2219 | } |
| 2220 | |
| 2221 | # if defined(FEAT_EVAL) |
| 2222 | switch (fmt_spec) |
| 2223 | { |
| 2224 | case 'd': case 'u': case 'o': case 'x': case 'X': |
| 2225 | if (tvs != NULL && length_modifier == '\0') |
| 2226 | length_modifier = 'L'; |
| 2227 | } |
| 2228 | # endif |
| 2229 | |
| 2230 | // get parameter value, do initial processing |
| 2231 | switch (fmt_spec) |
| 2232 | { |
| 2233 | // '%' and 'c' behave similar to 's' regarding flags and field |
| 2234 | // widths |
| 2235 | case '%': |
| 2236 | case 'c': |
| 2237 | case 's': |
| 2238 | case 'S': |
| 2239 | str_arg_l = 1; |
| 2240 | switch (fmt_spec) |
| 2241 | { |
| 2242 | case '%': |
| 2243 | str_arg = p; |
| 2244 | break; |
| 2245 | |
| 2246 | case 'c': |
| 2247 | { |
| 2248 | int j; |
| 2249 | |
| 2250 | j = |
| 2251 | # if defined(FEAT_EVAL) |
| 2252 | tvs != NULL ? tv_nr(tvs, &arg_idx) : |
| 2253 | # endif |
| 2254 | va_arg(ap, int); |
| 2255 | // standard demands unsigned char |
| 2256 | uchar_arg = (unsigned char)j; |
| 2257 | str_arg = (char *)&uchar_arg; |
| 2258 | break; |
| 2259 | } |
| 2260 | |
| 2261 | case 's': |
| 2262 | case 'S': |
| 2263 | str_arg = |
| 2264 | # if defined(FEAT_EVAL) |
| 2265 | tvs != NULL ? tv_str(tvs, &arg_idx, &tofree) : |
| 2266 | # endif |
| 2267 | va_arg(ap, char *); |
| 2268 | if (str_arg == NULL) |
| 2269 | { |
| 2270 | str_arg = "[NULL]"; |
| 2271 | str_arg_l = 6; |
| 2272 | } |
| 2273 | // make sure not to address string beyond the specified |
| 2274 | // precision !!! |
| 2275 | else if (!precision_specified) |
| 2276 | str_arg_l = strlen(str_arg); |
| 2277 | // truncate string if necessary as requested by precision |
| 2278 | else if (precision == 0) |
| 2279 | str_arg_l = 0; |
| 2280 | else |
| 2281 | { |
| 2282 | // Don't put the #if inside memchr(), it can be a |
| 2283 | // macro. |
| 2284 | // memchr on HP does not like n > 2^31 !!! |
| 2285 | char *q = memchr(str_arg, '\0', |
| 2286 | precision <= (size_t)0x7fffffffL ? precision |
| 2287 | : (size_t)0x7fffffffL); |
presuku | d85fccd | 2021-11-20 19:38:31 +0000 | [diff] [blame] | 2288 | |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2289 | str_arg_l = (q == NULL) ? precision |
| 2290 | : (size_t)(q - str_arg); |
| 2291 | } |
| 2292 | if (fmt_spec == 'S') |
| 2293 | { |
presuku | 1f2453f | 2021-11-24 15:32:57 +0000 | [diff] [blame] | 2294 | char_u *p1; |
| 2295 | size_t i; |
| 2296 | int cell; |
presuku | d85fccd | 2021-11-20 19:38:31 +0000 | [diff] [blame] | 2297 | |
presuku | 1f2453f | 2021-11-24 15:32:57 +0000 | [diff] [blame] | 2298 | for (i = 0, p1 = (char_u *)str_arg; *p1; |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2299 | p1 += mb_ptr2len(p1)) |
presuku | 1f2453f | 2021-11-24 15:32:57 +0000 | [diff] [blame] | 2300 | { |
| 2301 | cell = mb_ptr2cells(p1); |
| 2302 | if (precision_specified && i + cell > precision) |
| 2303 | break; |
| 2304 | i += cell; |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2305 | } |
presuku | 1f2453f | 2021-11-24 15:32:57 +0000 | [diff] [blame] | 2306 | |
| 2307 | str_arg_l = p1 - (char_u *)str_arg; |
presuku | d85fccd | 2021-11-20 19:38:31 +0000 | [diff] [blame] | 2308 | if (min_field_width != 0) |
presuku | 1f2453f | 2021-11-24 15:32:57 +0000 | [diff] [blame] | 2309 | min_field_width += str_arg_l - i; |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2310 | } |
| 2311 | break; |
| 2312 | |
| 2313 | default: |
| 2314 | break; |
| 2315 | } |
| 2316 | break; |
| 2317 | |
| 2318 | case 'd': case 'u': |
| 2319 | case 'b': case 'B': |
| 2320 | case 'o': |
| 2321 | case 'x': case 'X': |
| 2322 | case 'p': |
| 2323 | { |
| 2324 | // NOTE: the u, b, o, x, X and p conversion specifiers |
| 2325 | // imply the value is unsigned; d implies a signed |
| 2326 | // value |
| 2327 | |
| 2328 | // 0 if numeric argument is zero (or if pointer is |
| 2329 | // NULL for 'p'), +1 if greater than zero (or nonzero |
| 2330 | // for unsigned arguments), -1 if negative (unsigned |
| 2331 | // argument is never negative) |
| 2332 | int arg_sign = 0; |
| 2333 | |
| 2334 | // only set for length modifier h, or for no length |
| 2335 | // modifiers |
| 2336 | int int_arg = 0; |
| 2337 | unsigned int uint_arg = 0; |
| 2338 | |
| 2339 | // only set for length modifier l |
| 2340 | long int long_arg = 0; |
| 2341 | unsigned long int ulong_arg = 0; |
| 2342 | |
| 2343 | // only set for length modifier ll |
| 2344 | varnumber_T llong_arg = 0; |
| 2345 | uvarnumber_T ullong_arg = 0; |
| 2346 | |
| 2347 | // only set for b conversion |
| 2348 | uvarnumber_T bin_arg = 0; |
| 2349 | |
| 2350 | // pointer argument value -only defined for p |
| 2351 | // conversion |
| 2352 | void *ptr_arg = NULL; |
| 2353 | |
| 2354 | if (fmt_spec == 'p') |
| 2355 | { |
| 2356 | length_modifier = '\0'; |
| 2357 | ptr_arg = |
| 2358 | # if defined(FEAT_EVAL) |
| 2359 | tvs != NULL ? (void *)tv_str(tvs, &arg_idx, |
| 2360 | NULL) : |
| 2361 | # endif |
| 2362 | va_arg(ap, void *); |
| 2363 | if (ptr_arg != NULL) |
| 2364 | arg_sign = 1; |
| 2365 | } |
| 2366 | else if (fmt_spec == 'b' || fmt_spec == 'B') |
| 2367 | { |
| 2368 | bin_arg = |
| 2369 | # if defined(FEAT_EVAL) |
| 2370 | tvs != NULL ? |
| 2371 | (uvarnumber_T)tv_nr(tvs, &arg_idx) : |
| 2372 | # endif |
| 2373 | va_arg(ap, uvarnumber_T); |
| 2374 | if (bin_arg != 0) |
| 2375 | arg_sign = 1; |
| 2376 | } |
| 2377 | else if (fmt_spec == 'd') |
| 2378 | { |
| 2379 | // signed |
| 2380 | switch (length_modifier) |
| 2381 | { |
| 2382 | case '\0': |
| 2383 | case 'h': |
| 2384 | // char and short arguments are passed as int. |
| 2385 | int_arg = |
| 2386 | # if defined(FEAT_EVAL) |
| 2387 | tvs != NULL ? tv_nr(tvs, &arg_idx) : |
| 2388 | # endif |
| 2389 | va_arg(ap, int); |
| 2390 | if (int_arg > 0) |
| 2391 | arg_sign = 1; |
| 2392 | else if (int_arg < 0) |
| 2393 | arg_sign = -1; |
| 2394 | break; |
| 2395 | case 'l': |
| 2396 | long_arg = |
| 2397 | # if defined(FEAT_EVAL) |
| 2398 | tvs != NULL ? tv_nr(tvs, &arg_idx) : |
| 2399 | # endif |
| 2400 | va_arg(ap, long int); |
| 2401 | if (long_arg > 0) |
| 2402 | arg_sign = 1; |
| 2403 | else if (long_arg < 0) |
| 2404 | arg_sign = -1; |
| 2405 | break; |
| 2406 | case 'L': |
| 2407 | llong_arg = |
| 2408 | # if defined(FEAT_EVAL) |
| 2409 | tvs != NULL ? tv_nr(tvs, &arg_idx) : |
| 2410 | # endif |
| 2411 | va_arg(ap, varnumber_T); |
| 2412 | if (llong_arg > 0) |
| 2413 | arg_sign = 1; |
| 2414 | else if (llong_arg < 0) |
| 2415 | arg_sign = -1; |
| 2416 | break; |
| 2417 | } |
| 2418 | } |
| 2419 | else |
| 2420 | { |
| 2421 | // unsigned |
| 2422 | switch (length_modifier) |
| 2423 | { |
| 2424 | case '\0': |
| 2425 | case 'h': |
| 2426 | uint_arg = |
| 2427 | # if defined(FEAT_EVAL) |
| 2428 | tvs != NULL ? (unsigned) |
| 2429 | tv_nr(tvs, &arg_idx) : |
| 2430 | # endif |
| 2431 | va_arg(ap, unsigned int); |
| 2432 | if (uint_arg != 0) |
| 2433 | arg_sign = 1; |
| 2434 | break; |
| 2435 | case 'l': |
| 2436 | ulong_arg = |
| 2437 | # if defined(FEAT_EVAL) |
| 2438 | tvs != NULL ? (unsigned long) |
| 2439 | tv_nr(tvs, &arg_idx) : |
| 2440 | # endif |
| 2441 | va_arg(ap, unsigned long int); |
| 2442 | if (ulong_arg != 0) |
| 2443 | arg_sign = 1; |
| 2444 | break; |
| 2445 | case 'L': |
| 2446 | ullong_arg = |
| 2447 | # if defined(FEAT_EVAL) |
| 2448 | tvs != NULL ? (uvarnumber_T) |
| 2449 | tv_nr(tvs, &arg_idx) : |
| 2450 | # endif |
| 2451 | va_arg(ap, uvarnumber_T); |
| 2452 | if (ullong_arg != 0) |
| 2453 | arg_sign = 1; |
| 2454 | break; |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | str_arg = tmp; |
| 2459 | str_arg_l = 0; |
| 2460 | |
| 2461 | // NOTE: |
| 2462 | // For d, i, u, o, x, and X conversions, if precision is |
| 2463 | // specified, the '0' flag should be ignored. This is so |
| 2464 | // with Solaris 2.6, Digital UNIX 4.0, HPUX 10, Linux, |
| 2465 | // FreeBSD, NetBSD; but not with Perl. |
| 2466 | if (precision_specified) |
| 2467 | zero_padding = 0; |
| 2468 | if (fmt_spec == 'd') |
| 2469 | { |
| 2470 | if (force_sign && arg_sign >= 0) |
| 2471 | tmp[str_arg_l++] = space_for_positive ? ' ' : '+'; |
| 2472 | // leave negative numbers for sprintf to handle, to |
| 2473 | // avoid handling tricky cases like (short int)-32768 |
| 2474 | } |
| 2475 | else if (alternate_form) |
| 2476 | { |
| 2477 | if (arg_sign != 0 |
| 2478 | && (fmt_spec == 'b' || fmt_spec == 'B' |
| 2479 | || fmt_spec == 'x' || fmt_spec == 'X') ) |
| 2480 | { |
| 2481 | tmp[str_arg_l++] = '0'; |
| 2482 | tmp[str_arg_l++] = fmt_spec; |
| 2483 | } |
| 2484 | // alternate form should have no effect for p |
| 2485 | // conversion, but ... |
| 2486 | } |
| 2487 | |
| 2488 | zero_padding_insertion_ind = str_arg_l; |
| 2489 | if (!precision_specified) |
| 2490 | precision = 1; // default precision is 1 |
| 2491 | if (precision == 0 && arg_sign == 0) |
| 2492 | { |
| 2493 | // When zero value is formatted with an explicit |
| 2494 | // precision 0, the resulting formatted string is |
| 2495 | // empty (d, i, u, b, B, o, x, X, p). |
| 2496 | } |
| 2497 | else |
| 2498 | { |
| 2499 | char f[6]; |
| 2500 | int f_l = 0; |
| 2501 | |
| 2502 | // construct a simple format string for sprintf |
| 2503 | f[f_l++] = '%'; |
| 2504 | if (!length_modifier) |
| 2505 | ; |
| 2506 | else if (length_modifier == 'L') |
| 2507 | { |
| 2508 | # ifdef MSWIN |
| 2509 | f[f_l++] = 'I'; |
| 2510 | f[f_l++] = '6'; |
| 2511 | f[f_l++] = '4'; |
| 2512 | # else |
| 2513 | f[f_l++] = 'l'; |
| 2514 | f[f_l++] = 'l'; |
| 2515 | # endif |
| 2516 | } |
| 2517 | else |
| 2518 | f[f_l++] = length_modifier; |
| 2519 | f[f_l++] = fmt_spec; |
| 2520 | f[f_l++] = '\0'; |
| 2521 | |
| 2522 | if (fmt_spec == 'p') |
| 2523 | str_arg_l += sprintf(tmp + str_arg_l, f, ptr_arg); |
| 2524 | else if (fmt_spec == 'b' || fmt_spec == 'B') |
| 2525 | { |
| 2526 | char b[8 * sizeof(uvarnumber_T)]; |
| 2527 | size_t b_l = 0; |
| 2528 | uvarnumber_T bn = bin_arg; |
| 2529 | |
| 2530 | do |
| 2531 | { |
| 2532 | b[sizeof(b) - ++b_l] = '0' + (bn & 0x1); |
| 2533 | bn >>= 1; |
| 2534 | } |
| 2535 | while (bn != 0); |
| 2536 | |
| 2537 | memcpy(tmp + str_arg_l, b + sizeof(b) - b_l, b_l); |
| 2538 | str_arg_l += b_l; |
| 2539 | } |
| 2540 | else if (fmt_spec == 'd') |
| 2541 | { |
| 2542 | // signed |
| 2543 | switch (length_modifier) |
| 2544 | { |
| 2545 | case '\0': str_arg_l += sprintf( |
| 2546 | tmp + str_arg_l, f, |
| 2547 | int_arg); |
| 2548 | break; |
| 2549 | case 'h': str_arg_l += sprintf( |
| 2550 | tmp + str_arg_l, f, |
| 2551 | (short)int_arg); |
| 2552 | break; |
| 2553 | case 'l': str_arg_l += sprintf( |
| 2554 | tmp + str_arg_l, f, long_arg); |
| 2555 | break; |
| 2556 | case 'L': str_arg_l += sprintf( |
| 2557 | tmp + str_arg_l, f, llong_arg); |
| 2558 | break; |
| 2559 | } |
| 2560 | } |
| 2561 | else |
| 2562 | { |
| 2563 | // unsigned |
| 2564 | switch (length_modifier) |
| 2565 | { |
| 2566 | case '\0': str_arg_l += sprintf( |
| 2567 | tmp + str_arg_l, f, |
| 2568 | uint_arg); |
| 2569 | break; |
| 2570 | case 'h': str_arg_l += sprintf( |
| 2571 | tmp + str_arg_l, f, |
| 2572 | (unsigned short)uint_arg); |
| 2573 | break; |
| 2574 | case 'l': str_arg_l += sprintf( |
| 2575 | tmp + str_arg_l, f, ulong_arg); |
| 2576 | break; |
| 2577 | case 'L': str_arg_l += sprintf( |
| 2578 | tmp + str_arg_l, f, ullong_arg); |
| 2579 | break; |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | // include the optional minus sign and possible |
| 2584 | // "0x" in the region before the zero padding |
| 2585 | // insertion point |
| 2586 | if (zero_padding_insertion_ind < str_arg_l |
| 2587 | && tmp[zero_padding_insertion_ind] == '-') |
| 2588 | zero_padding_insertion_ind++; |
| 2589 | if (zero_padding_insertion_ind + 1 < str_arg_l |
| 2590 | && tmp[zero_padding_insertion_ind] == '0' |
| 2591 | && (tmp[zero_padding_insertion_ind + 1] == 'x' |
| 2592 | || tmp[zero_padding_insertion_ind + 1] == 'X')) |
| 2593 | zero_padding_insertion_ind += 2; |
| 2594 | } |
| 2595 | |
| 2596 | { |
| 2597 | size_t num_of_digits = str_arg_l |
| 2598 | - zero_padding_insertion_ind; |
| 2599 | |
| 2600 | if (alternate_form && fmt_spec == 'o' |
| 2601 | // unless zero is already the first |
| 2602 | // character |
| 2603 | && !(zero_padding_insertion_ind < str_arg_l |
| 2604 | && tmp[zero_padding_insertion_ind] == '0')) |
| 2605 | { |
| 2606 | // assure leading zero for alternate-form |
| 2607 | // octal numbers |
| 2608 | if (!precision_specified |
| 2609 | || precision < num_of_digits + 1) |
| 2610 | { |
| 2611 | // precision is increased to force the |
| 2612 | // first character to be zero, except if a |
| 2613 | // zero value is formatted with an |
| 2614 | // explicit precision of zero |
| 2615 | precision = num_of_digits + 1; |
| 2616 | } |
| 2617 | } |
| 2618 | // zero padding to specified precision? |
| 2619 | if (num_of_digits < precision) |
| 2620 | number_of_zeros_to_pad = precision - num_of_digits; |
| 2621 | } |
| 2622 | // zero padding to specified minimal field width? |
| 2623 | if (!justify_left && zero_padding) |
| 2624 | { |
| 2625 | int n = (int)(min_field_width - (str_arg_l |
| 2626 | + number_of_zeros_to_pad)); |
| 2627 | if (n > 0) |
| 2628 | number_of_zeros_to_pad += n; |
| 2629 | } |
| 2630 | break; |
| 2631 | } |
| 2632 | |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2633 | case 'f': |
| 2634 | case 'F': |
| 2635 | case 'e': |
| 2636 | case 'E': |
| 2637 | case 'g': |
| 2638 | case 'G': |
| 2639 | { |
| 2640 | // Floating point. |
| 2641 | double f; |
| 2642 | double abs_f; |
| 2643 | char format[40]; |
| 2644 | int l; |
| 2645 | int remove_trailing_zeroes = FALSE; |
| 2646 | |
| 2647 | f = |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 2648 | # if defined(FEAT_EVAL) |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2649 | tvs != NULL ? tv_float(tvs, &arg_idx) : |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 2650 | # endif |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2651 | va_arg(ap, double); |
| 2652 | abs_f = f < 0 ? -f : f; |
| 2653 | |
| 2654 | if (fmt_spec == 'g' || fmt_spec == 'G') |
| 2655 | { |
| 2656 | // Would be nice to use %g directly, but it prints |
| 2657 | // "1.0" as "1", we don't want that. |
| 2658 | if ((abs_f >= 0.001 && abs_f < 10000000.0) |
| 2659 | || abs_f == 0.0) |
| 2660 | fmt_spec = ASCII_ISUPPER(fmt_spec) ? 'F' : 'f'; |
| 2661 | else |
| 2662 | fmt_spec = fmt_spec == 'g' ? 'e' : 'E'; |
| 2663 | remove_trailing_zeroes = TRUE; |
| 2664 | } |
| 2665 | |
| 2666 | if ((fmt_spec == 'f' || fmt_spec == 'F') && |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 2667 | # ifdef VAX |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2668 | abs_f > 1.0e38 |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 2669 | # else |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2670 | abs_f > 1.0e307 |
Bram Moolenaar | 73e28dc | 2022-09-17 21:08:33 +0100 | [diff] [blame] | 2671 | # endif |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2672 | ) |
| 2673 | { |
| 2674 | // Avoid a buffer overflow |
| 2675 | STRCPY(tmp, infinity_str(f > 0.0, fmt_spec, |
| 2676 | force_sign, space_for_positive)); |
| 2677 | str_arg_l = STRLEN(tmp); |
| 2678 | zero_padding = 0; |
| 2679 | } |
| 2680 | else |
| 2681 | { |
| 2682 | if (isnan(f)) |
| 2683 | { |
| 2684 | // Not a number: nan or NAN |
| 2685 | STRCPY(tmp, ASCII_ISUPPER(fmt_spec) ? "NAN" |
| 2686 | : "nan"); |
| 2687 | str_arg_l = 3; |
| 2688 | zero_padding = 0; |
| 2689 | } |
| 2690 | else if (isinf(f)) |
| 2691 | { |
| 2692 | STRCPY(tmp, infinity_str(f > 0.0, fmt_spec, |
| 2693 | force_sign, space_for_positive)); |
| 2694 | str_arg_l = STRLEN(tmp); |
| 2695 | zero_padding = 0; |
| 2696 | } |
| 2697 | else |
| 2698 | { |
| 2699 | // Regular float number |
| 2700 | format[0] = '%'; |
| 2701 | l = 1; |
| 2702 | if (force_sign) |
| 2703 | format[l++] = space_for_positive ? ' ' : '+'; |
| 2704 | if (precision_specified) |
| 2705 | { |
| 2706 | size_t max_prec = TMP_LEN - 10; |
| 2707 | |
| 2708 | // Make sure we don't get more digits than we |
| 2709 | // have room for. |
| 2710 | if ((fmt_spec == 'f' || fmt_spec == 'F') |
| 2711 | && abs_f > 1.0) |
| 2712 | max_prec -= (size_t)log10(abs_f); |
| 2713 | if (precision > max_prec) |
| 2714 | precision = max_prec; |
| 2715 | l += sprintf(format + l, ".%d", (int)precision); |
| 2716 | } |
| 2717 | format[l] = fmt_spec == 'F' ? 'f' : fmt_spec; |
| 2718 | format[l + 1] = NUL; |
| 2719 | |
| 2720 | str_arg_l = sprintf(tmp, format, f); |
| 2721 | } |
| 2722 | |
| 2723 | if (remove_trailing_zeroes) |
| 2724 | { |
| 2725 | int i; |
| 2726 | char *tp; |
| 2727 | |
| 2728 | // Using %g or %G: remove superfluous zeroes. |
| 2729 | if (fmt_spec == 'f' || fmt_spec == 'F') |
| 2730 | tp = tmp + str_arg_l - 1; |
| 2731 | else |
| 2732 | { |
| 2733 | tp = (char *)vim_strchr((char_u *)tmp, |
| 2734 | fmt_spec == 'e' ? 'e' : 'E'); |
| 2735 | if (tp != NULL) |
| 2736 | { |
| 2737 | // Remove superfluous '+' and leading |
| 2738 | // zeroes from the exponent. |
| 2739 | if (tp[1] == '+') |
| 2740 | { |
| 2741 | // Change "1.0e+07" to "1.0e07" |
| 2742 | STRMOVE(tp + 1, tp + 2); |
| 2743 | --str_arg_l; |
| 2744 | } |
| 2745 | i = (tp[1] == '-') ? 2 : 1; |
| 2746 | while (tp[i] == '0') |
| 2747 | { |
| 2748 | // Change "1.0e07" to "1.0e7" |
| 2749 | STRMOVE(tp + i, tp + i + 1); |
| 2750 | --str_arg_l; |
| 2751 | } |
| 2752 | --tp; |
| 2753 | } |
| 2754 | } |
| 2755 | |
| 2756 | if (tp != NULL && !precision_specified) |
| 2757 | // Remove trailing zeroes, but keep the one |
| 2758 | // just after a dot. |
| 2759 | while (tp > tmp + 2 && *tp == '0' |
| 2760 | && tp[-1] != '.') |
| 2761 | { |
| 2762 | STRMOVE(tp, tp + 1); |
| 2763 | --tp; |
| 2764 | --str_arg_l; |
| 2765 | } |
| 2766 | } |
| 2767 | else |
| 2768 | { |
| 2769 | char *tp; |
| 2770 | |
| 2771 | // Be consistent: some printf("%e") use 1.0e+12 |
| 2772 | // and some 1.0e+012. Remove one zero in the last |
| 2773 | // case. |
| 2774 | tp = (char *)vim_strchr((char_u *)tmp, |
| 2775 | fmt_spec == 'e' ? 'e' : 'E'); |
| 2776 | if (tp != NULL && (tp[1] == '+' || tp[1] == '-') |
| 2777 | && tp[2] == '0' |
| 2778 | && vim_isdigit(tp[3]) |
| 2779 | && vim_isdigit(tp[4])) |
| 2780 | { |
| 2781 | STRMOVE(tp + 2, tp + 3); |
| 2782 | --str_arg_l; |
| 2783 | } |
| 2784 | } |
| 2785 | } |
| 2786 | if (zero_padding && min_field_width > str_arg_l |
| 2787 | && (tmp[0] == '-' || force_sign)) |
| 2788 | { |
| 2789 | // padding 0's should be inserted after the sign |
| 2790 | number_of_zeros_to_pad = min_field_width - str_arg_l; |
| 2791 | zero_padding_insertion_ind = 1; |
| 2792 | } |
| 2793 | str_arg = tmp; |
| 2794 | break; |
| 2795 | } |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2796 | |
| 2797 | default: |
| 2798 | // unrecognized conversion specifier, keep format string |
| 2799 | // as-is |
| 2800 | zero_padding = 0; // turn zero padding off for non-numeric |
| 2801 | // conversion |
| 2802 | justify_left = 1; |
| 2803 | min_field_width = 0; // reset flags |
| 2804 | |
| 2805 | // discard the unrecognized conversion, just keep * |
| 2806 | // the unrecognized conversion character |
| 2807 | str_arg = p; |
| 2808 | str_arg_l = 0; |
| 2809 | if (*p != NUL) |
| 2810 | str_arg_l++; // include invalid conversion specifier |
| 2811 | // unchanged if not at end-of-string |
| 2812 | break; |
| 2813 | } |
| 2814 | |
| 2815 | if (*p != NUL) |
| 2816 | p++; // step over the just processed conversion specifier |
| 2817 | |
| 2818 | // insert padding to the left as requested by min_field_width; |
| 2819 | // this does not include the zero padding in case of numerical |
| 2820 | // conversions |
| 2821 | if (!justify_left) |
| 2822 | { |
| 2823 | // left padding with blank or zero |
| 2824 | int pn = (int)(min_field_width - (str_arg_l + number_of_zeros_to_pad)); |
| 2825 | |
| 2826 | if (pn > 0) |
| 2827 | { |
| 2828 | if (str_l < str_m) |
| 2829 | { |
| 2830 | size_t avail = str_m - str_l; |
| 2831 | |
| 2832 | vim_memset(str + str_l, zero_padding ? '0' : ' ', |
| 2833 | (size_t)pn > avail ? avail |
| 2834 | : (size_t)pn); |
| 2835 | } |
| 2836 | str_l += pn; |
| 2837 | } |
| 2838 | } |
| 2839 | |
| 2840 | // zero padding as requested by the precision or by the minimal |
| 2841 | // field width for numeric conversions required? |
| 2842 | if (number_of_zeros_to_pad == 0) |
| 2843 | { |
| 2844 | // will not copy first part of numeric right now, * |
| 2845 | // force it to be copied later in its entirety |
| 2846 | zero_padding_insertion_ind = 0; |
| 2847 | } |
| 2848 | else |
| 2849 | { |
| 2850 | // insert first part of numerics (sign or '0x') before zero |
| 2851 | // padding |
| 2852 | int zn = (int)zero_padding_insertion_ind; |
| 2853 | |
| 2854 | if (zn > 0) |
| 2855 | { |
| 2856 | if (str_l < str_m) |
| 2857 | { |
| 2858 | size_t avail = str_m - str_l; |
| 2859 | |
| 2860 | mch_memmove(str + str_l, str_arg, |
| 2861 | (size_t)zn > avail ? avail |
| 2862 | : (size_t)zn); |
| 2863 | } |
| 2864 | str_l += zn; |
| 2865 | } |
| 2866 | |
| 2867 | // insert zero padding as requested by the precision or min |
| 2868 | // field width |
| 2869 | zn = (int)number_of_zeros_to_pad; |
| 2870 | if (zn > 0) |
| 2871 | { |
| 2872 | if (str_l < str_m) |
| 2873 | { |
| 2874 | size_t avail = str_m - str_l; |
| 2875 | |
| 2876 | vim_memset(str + str_l, '0', |
| 2877 | (size_t)zn > avail ? avail |
| 2878 | : (size_t)zn); |
| 2879 | } |
| 2880 | str_l += zn; |
| 2881 | } |
| 2882 | } |
| 2883 | |
| 2884 | // insert formatted string |
| 2885 | // (or as-is conversion specifier for unknown conversions) |
| 2886 | { |
| 2887 | int sn = (int)(str_arg_l - zero_padding_insertion_ind); |
| 2888 | |
| 2889 | if (sn > 0) |
| 2890 | { |
| 2891 | if (str_l < str_m) |
| 2892 | { |
| 2893 | size_t avail = str_m - str_l; |
| 2894 | |
| 2895 | mch_memmove(str + str_l, |
| 2896 | str_arg + zero_padding_insertion_ind, |
| 2897 | (size_t)sn > avail ? avail : (size_t)sn); |
| 2898 | } |
| 2899 | str_l += sn; |
| 2900 | } |
| 2901 | } |
| 2902 | |
| 2903 | // insert right padding |
| 2904 | if (justify_left) |
| 2905 | { |
| 2906 | // right blank padding to the field width |
| 2907 | int pn = (int)(min_field_width |
| 2908 | - (str_arg_l + number_of_zeros_to_pad)); |
| 2909 | |
| 2910 | if (pn > 0) |
| 2911 | { |
| 2912 | if (str_l < str_m) |
| 2913 | { |
| 2914 | size_t avail = str_m - str_l; |
| 2915 | |
| 2916 | vim_memset(str + str_l, ' ', |
| 2917 | (size_t)pn > avail ? avail |
| 2918 | : (size_t)pn); |
| 2919 | } |
| 2920 | str_l += pn; |
| 2921 | } |
| 2922 | } |
| 2923 | vim_free(tofree); |
| 2924 | } |
| 2925 | } |
| 2926 | |
| 2927 | if (str_m > 0) |
| 2928 | { |
| 2929 | // make sure the string is nul-terminated even at the expense of |
| 2930 | // overwriting the last character (shouldn't happen, but just in case) |
| 2931 | // |
| 2932 | str[str_l <= str_m - 1 ? str_l : str_m - 1] = '\0'; |
| 2933 | } |
| 2934 | |
| 2935 | if (tvs != NULL && tvs[arg_idx - 1].v_type != VAR_UNKNOWN) |
Bram Moolenaar | 677658a | 2022-01-05 16:09:06 +0000 | [diff] [blame] | 2936 | emsg(_(e_too_many_arguments_to_printf)); |
Yegappan Lakshmanan | 8ee52af | 2021-08-09 19:59:06 +0200 | [diff] [blame] | 2937 | |
| 2938 | // Return the number of characters formatted (excluding trailing nul |
| 2939 | // character), that is, the number of characters that would have been |
| 2940 | // written to the buffer if it were large enough. |
| 2941 | return (int)str_l; |
| 2942 | } |
| 2943 | |
| 2944 | #endif // PROTO |