robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1/// @file charset.c 2/// 3/// Code related to character sets. 4 5#include <assert.h> 6#include <errno.h> 7#include <inttypes.h> 8#include <limits.h> 9#include <stdlib.h> 10#include <string.h> 11#include <uv.h> 12 13#include "auto/config.h" 14#include "klib/kvec.h" 15#include "nvim/ascii_defs.h" 16#include "nvim/buffer_defs.h" 17#include "nvim/charset.h" 18#include "nvim/cursor.h" 19#include "nvim/eval/typval_defs.h" 20#include "nvim/garray.h" 21#include "nvim/garray_defs.h" 22#include "nvim/globals.h" 23#include "nvim/keycodes.h" 24#include "nvim/macros_defs.h" 25#include "nvim/mbyte.h" 26#include "nvim/memory.h" 27#include "nvim/option.h" 28#include "nvim/path.h" 29#include "nvim/pos_defs.h" 30#include "nvim/strings.h" 31#include "nvim/types_defs.h" 32#include "nvim/vim_defs.h" 33 34#include "charset.c.generated.h" 35 36// a chartab is an array with 256 bits, each bit representing one of the characters 0-255. 37#define SET_CHARTAB (chartab ,c ) (chartab)[(unsigned)(c) >> 6] |= (1ull << ((c) & 0x3f)) 38#define RESET_CHARTAB (chartab ,c ) (chartab)[(unsigned)(c) >> 6] &= ~(1ull << ((c) & 0x3f)) 39#define GET_CHARTAB (chartab ,c ) ((chartab)[(unsigned)(c) >> 6] & (1ull << ((c) & 0x3f))) 40 41static uint64_t isf_chartab [4 ];// chartab for 'isfname' 42static uint64_t isi_chartab [4 ];// chartab for 'isindent' 43 44int buf_init_isk_chartab (buf_T * buf ) 45{ 46uint64_t save_chartab [4 ]; 47memcpy (save_chartab ,buf -> b_chartab ,sizeof save_chartab ); 48memset (buf -> b_chartab ,0 ,sizeof buf -> b_chartab ); 49if (buf -> b_p_lisp ) {// don't ask 50SET_CHARTAB (buf -> b_chartab ,'-' ); 51 } 52if (parse_isopt (buf -> b_p_isk ,buf -> b_chartab )== FAIL ) { 53memcpy (buf -> b_chartab ,save_chartab ,sizeof save_chartab ); 54return FAIL ; 55 } 56return OK ; 57} 58 59int init_isf_chartab (void ) 60{ 61uint64_t save_chartab [4 ]; 62memcpy (save_chartab ,isf_chartab ,sizeof save_chartab ); 63memset (isf_chartab ,0 ,sizeof isf_chartab ); 64isf_chartab [2 ]= 0xFFFFFFFF00000000 ;// there is no need 65isf_chartab [3 ]= 0xFFFFFFFFFFFFFFFF ;// to worry 66if (parse_isopt (p_isf ,isf_chartab )== FAIL ) { 67memcpy (isf_chartab ,save_chartab ,sizeof save_chartab ); 68return FAIL ; 69 } 70return OK ; 71} 72 73int init_isi_chartab (void ) 74{ 75uint64_t save_chartab [4 ]; 76memcpy (save_chartab ,isi_chartab ,sizeof save_chartab ); 77memset (isi_chartab ,0 ,sizeof isi_chartab ); 78if (parse_isopt (p_isi ,isi_chartab )== FAIL ) { 79memcpy (isi_chartab ,save_chartab ,sizeof save_chartab ); 80return FAIL ; 81 } 82return OK ; 83} 84 85/// Checks the format for the option settings 'iskeyword', 'isident', 'isfname' 86/// Returns FAIL if has an error, OK otherwise. 87int check_isopt (char * var ) 88{ 89return parse_isopt (var ,NULL ); 90} 91 92/// @param chartab if NULL only check if option string is valid 93int parse_isopt (const char * var ,uint64_t * chartab ) 94{ 95const char * p = var ; 96 97// Parses the 'isident', 'iskeyword', 'isfname' options. 98// Each option is a list of characters, character numbers or ranges, 99// separated by commas, e.g.: "200-210,x,#-178,-" 100while (* p ) { 101bool tilde = false; 102bool do_isalpha = false; 103 104if (* p == '^' && p [1 ]!= NUL ) { 105tilde = true; 106p ++ ; 107 } 108 109int c ; 110if (ascii_isdigit (* p )) { 111c = getdigits_int ((char ** )& p , true,0 ); 112 }else { 113c = mb_ptr2char_adv (& p ); 114 } 115int c2 = -1 ; 116 117if (* p == '-' && p [1 ]!= NUL ) { 118p ++ ; 119 120if (ascii_isdigit (* p )) { 121c2 = getdigits_int ((char ** )& p , true,0 ); 122 }else { 123c2 = mb_ptr2char_adv (& p ); 124 } 125 } 126 127if (c <=0 || c >=256 || (c2 < c && c2 != -1 )|| c2 >=256 128|| !(* p == NUL || * p == ',' )) { 129return FAIL ; 130 } 131 132bool trail_comma = * p == ',' ; 133p = skip_to_option_part (p ); 134if (trail_comma && * p == NUL ) { 135// Trailing comma is not allowed. 136return FAIL ; 137 } 138 139if (chartab == NULL ) {// only check! 140continue ; 141 } 142 143if (c2 == -1 ) {// not a range 144// A single '@' (not "@-@"): 145// Decide on letters being ID/printable/keyword chars with 146// standard function isalpha(). This takes care of locale for 147// single-byte characters). 148if (c == '@' ) { 149do_isalpha = true; 150c = 1 ; 151c2 = 255 ; 152 }else { 153c2 = c ; 154 } 155 } 156 157while (c <=c2 ) { 158// Use the MB_ functions here, because isalpha() doesn't 159// work properly when 'encoding' is "latin1" and the locale is 160// "C". 161if (!do_isalpha || mb_islower (c )|| mb_isupper (c )) { 162if (tilde ) { 163RESET_CHARTAB (chartab ,c ); 164 }else { 165SET_CHARTAB (chartab ,c ); 166 } 167 } 168c ++ ; 169 } 170 } 171 172return OK ; 173} 174 175/// Translate any special characters in buf[bufsize] in-place. 176/// 177/// The result is a string with only printable characters, but if there is not 178/// enough room, not all characters will be translated. 179/// 180/// @param buf 181/// @param bufsize 182void trans_characters (char * buf ,int bufsize ) 183{ 184char * trs ;// translated character 185int len = (int )strlen (buf );// length of string needing translation 186int room = bufsize - len ;// room in buffer after string 187 188while (* buf != 0 ) { 189int trs_len ;// length of trs[] 190// Assume a multi-byte character doesn't need translation. 191if ((trs_len = utfc_ptr2len (buf ))> 1 ) { 192len -= trs_len ; 193 }else { 194trs = transchar_byte ((uint8_t )(* buf )); 195trs_len = (int )strlen (trs ); 196 197if (trs_len > 1 ) { 198room -= trs_len - 1 ; 199if (room <=0 ) { 200return ; 201 } 202memmove (buf + trs_len ,buf + 1 , (size_t )len ); 203 } 204memmove (buf ,trs , (size_t )trs_len ); 205len -- ; 206 } 207buf += trs_len ; 208 } 209} 210 211/// Find length of a string capable of holding s with all specials replaced 212/// 213/// Assumes replacing special characters with printable ones just like 214/// strtrans() does. 215/// 216/// @param[in] s String to check. 217/// 218/// @return number of bytes needed to hold a translation of `s`, NUL byte not 219/// included. 220size_t transstr_len (const char * const s ,bool untab ) 221FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_PURE 222{ 223const char * p = s ; 224size_t len = 0 ; 225 226while (* p ) { 227const size_t l = (size_t )utfc_ptr2len (p ); 228if (l > 1 ) { 229if (vim_isprintc (utf_ptr2char (p ))) { 230len += l ; 231 }else { 232for (size_t off = 0 ;off < l ;off += (size_t )utf_ptr2len (p + off )) { 233int c = utf_ptr2char (p + off ); 234char hexbuf [9 ]; 235len += transchar_hex (hexbuf ,c ); 236 } 237 } 238p += l ; 239 }else if (* p == TAB && !untab ) { 240len += 1 ; 241p ++ ; 242 }else { 243// Illegal byte sequence may occupy up to 4 characters. 244uint8_t c = (uint8_t )(* p ++ ); 245len += (size_t )(c < 0x80 ?ascii2cells (c ) :4 ); 246 } 247 } 248return len ; 249} 250 251/// Replace special characters with printable ones 252/// 253/// @param[in] s String to replace characters from. 254/// @param[out] buf Buffer to which result should be saved. 255/// @param[in] len Buffer length. Resulting string may not occupy more then 256/// len - 1 bytes (one for trailing NUL byte). 257/// @param[in] untab remove tab characters 258/// 259/// @return length of the resulting string, without the NUL byte. 260size_t transstr_buf (const char * const s ,const ssize_t slen ,char * const buf ,const size_t buflen , 261bool untab ) 262FUNC_ATTR_NONNULL_ALL 263{ 264const char * p = s ; 265char * buf_p = buf ; 266char * const buf_e = buf_p + buflen - 1 ; 267 268while ((slen < 0 || (p - s )< slen )&& * p != NUL && buf_p < buf_e ) { 269const size_t l = (size_t )utfc_ptr2len (p ); 270if (l > 1 ) { 271if (buf_p + l > buf_e ) { 272break ;// Exceeded `buf` size. 273 } 274 275if (vim_isprintc (utf_ptr2char (p ))) { 276memmove (buf_p ,p ,l ); 277buf_p += l ; 278 }else { 279for (size_t off = 0 ;off < l ;off += (size_t )utf_ptr2len (p + off )) { 280int c = utf_ptr2char (p + off ); 281char hexbuf [9 ];// <up to 6 bytes>NUL 282const size_t hexlen = transchar_hex (hexbuf ,c ); 283if (buf_p + hexlen > buf_e ) { 284break ; 285 } 286memmove (buf_p ,hexbuf ,hexlen ); 287buf_p += hexlen ; 288 } 289 } 290p += l ; 291 }else if (* p == TAB && !untab ) { 292* buf_p ++ = * p ++ ; 293 }else { 294const char * const tb = transchar_byte ((uint8_t )(* p ++ )); 295const size_t tb_len = strlen (tb ); 296if (buf_p + tb_len > buf_e ) { 297break ;// Exceeded `buf` size. 298 } 299memmove (buf_p ,tb ,tb_len ); 300buf_p += tb_len ; 301 } 302 } 303* buf_p = NUL ; 304assert (buf_p <=buf_e ); 305return (size_t )(buf_p - buf ); 306} 307 308/// Copy string and replace special characters with printable characters 309/// 310/// Works like `strtrans()` does, used for that and in some other places. 311/// 312/// @param[in] s String to replace characters from. 313/// 314/// @return [allocated] translated string 315char * transstr (const char * const s ,bool untab ) 316FUNC_ATTR_NONNULL_RET 317{ 318// Compute the length of the result, taking account of unprintable 319// multi-byte characters. 320const size_t len = transstr_len (s ,untab )+ 1 ; 321char * const buf = xmalloc (len ); 322transstr_buf (s ,-1 ,buf ,len ,untab ); 323return buf ; 324} 325 326size_t kv_transstr (StringBuilder * str ,const char * const s ,bool untab ) 327FUNC_ATTR_NONNULL_ARG (1 ) 328{ 329if (!s ) { 330return 0 ; 331 } 332 333// Compute the length of the result, taking account of unprintable 334// multi-byte characters. 335const size_t len = transstr_len (s ,untab ); 336kv_ensure_space (* str ,len + 1 ); 337transstr_buf (s ,-1 ,str -> items + str -> size ,len + 1 ,untab ); 338str -> size += len ;// do not include NUL byte 339return len ; 340} 341 342/// Convert the string "str[orglen]" to do ignore-case comparing. 343/// Use the current locale. 344/// 345/// When "buf" is NULL, return an allocated string. 346/// Otherwise, put the result in buf, limited by buflen, and return buf. 347char * str_foldcase (char * str ,int orglen ,char * buf ,int buflen ) 348FUNC_ATTR_NONNULL_RET 349{ 350garray_T ga ; 351int len = orglen ; 352 353#define GA_CHAR (i ) ((char *)ga.ga_data)[i] 354#define GA_PTR (i ) ((char *)ga.ga_data + (i)) 355#define STR_CHAR (i ) (buf == NULL ? GA_CHAR(i) : buf[i]) 356#define STR_PTR (i ) (buf == NULL ? GA_PTR(i) : buf + (i)) 357 358// Copy "str" into "buf" or allocated memory, unmodified. 359if (buf == NULL ) { 360ga_init (& ga ,1 ,10 ); 361 362ga_grow (& ga ,len + 1 ); 363memmove (ga .ga_data ,str , (size_t )len ); 364ga .ga_len = len ; 365 }else { 366if (len >=buflen ) { 367// Ugly! 368len = buflen - 1 ; 369 } 370memmove (buf ,str , (size_t )len ); 371 } 372 373if (buf == NULL ) { 374GA_CHAR (len )= NUL ; 375 }else { 376buf [len ]= NUL ; 377 } 378 379// Make each character lower case. 380int i = 0 ; 381while (STR_CHAR (i )!= NUL ) { 382int c = utf_ptr2char (STR_PTR (i )); 383int olen = utf_ptr2len (STR_PTR (i )); 384int lc = mb_tolower (c ); 385 386// Only replace the character when it is not an invalid 387// sequence (ASCII character or more than one byte) and 388// mb_tolower() doesn't return the original character. 389if (((c < 0x80 )|| (olen > 1 ))&& (c != lc )) { 390int nlen = utf_char2len (lc ); 391 392// If the byte length changes need to shift the following 393// characters forward or backward. 394if (olen != nlen ) { 395if (nlen > olen ) { 396if (buf == NULL ) { 397ga_grow (& ga ,nlen - olen + 1 ); 398 }else { 399if (len + nlen - olen >=buflen ) { 400// out of memory, keep old char 401lc = c ; 402nlen = olen ; 403 } 404 } 405 } 406 407if (olen != nlen ) { 408if (buf == NULL ) { 409STRMOVE (GA_PTR (i )+ nlen ,GA_PTR (i )+ olen ); 410ga .ga_len += nlen - olen ; 411 }else { 412STRMOVE (buf + i + nlen ,buf + i + olen ); 413len += nlen - olen ; 414 } 415 } 416 } 417utf_char2bytes (lc ,STR_PTR (i )); 418 } 419 420// skip to next multi-byte char 421i += utfc_ptr2len (STR_PTR (i )); 422 } 423 424if (buf == NULL ) { 425return ga .ga_data ; 426 } 427return buf ; 428} 429 430// Does NOT work for multi-byte characters, c must be <= 255. 431// Also doesn't work for the first byte of a multi-byte, "c" must be a 432// character! 433static uint8_t transchar_charbuf [11 ]; 434 435/// Translate a character into a printable one, leaving printable ASCII intact 436/// 437/// All unicode characters are considered non-printable in this function. 438/// 439/// @param[in] c Character to translate. 440/// 441/// @return translated character into a static buffer. 442char * transchar (int c ) 443{ 444return transchar_buf (curbuf ,c ); 445} 446 447char * transchar_buf (const buf_T * buf ,int c ) 448{ 449int i = 0 ; 450if (IS_SPECIAL (c )) { 451// special key code, display as ~@ char 452transchar_charbuf [0 ]= '~' ; 453transchar_charbuf [1 ]= '@' ; 454i = 2 ; 455c = K_SECOND (c ); 456 } 457 458if ((c <=0xFF )&& vim_isprintc (c )) { 459// printable character 460transchar_charbuf [i ]= (uint8_t )c ; 461transchar_charbuf [i + 1 ]= NUL ; 462 }else if (c <=0xFF ) { 463transchar_nonprint (buf , (char * )transchar_charbuf + i ,c ); 464 }else { 465transchar_hex ((char * )transchar_charbuf + i ,c ); 466 } 467return (char * )transchar_charbuf ; 468} 469 470/// Like transchar(), but called with a byte instead of a character. 471/// 472/// Checks for an illegal UTF-8 byte. Uses 'fileformat' of the current buffer. 473/// 474/// @param[in] c Byte to translate. 475/// 476/// @return pointer to translated character in transchar_charbuf. 477char * transchar_byte (const int c ) 478FUNC_ATTR_WARN_UNUSED_RESULT 479{ 480return transchar_byte_buf (curbuf ,c ); 481} 482 483/// Like transchar_buf(), but called with a byte instead of a character. 484/// 485/// Checks for an illegal UTF-8 byte. Uses 'fileformat' of "buf", unless it is NULL. 486/// 487/// @param[in] c Byte to translate. 488/// 489/// @return pointer to translated character in transchar_charbuf. 490char * transchar_byte_buf (const buf_T * buf ,const int c ) 491FUNC_ATTR_WARN_UNUSED_RESULT 492{ 493if (c >=0x80 ) { 494transchar_nonprint (buf , (char * )transchar_charbuf ,c ); 495return (char * )transchar_charbuf ; 496 } 497return transchar_buf (buf ,c ); 498} 499 500/// Convert non-printable characters to 2..4 printable ones 501/// 502/// @warning Does not work for multi-byte characters, c must be <= 255. 503/// 504/// @param[in] buf Required to check the file format 505/// @param[out] charbuf Buffer to store result in, must be able to hold 506/// at least 5 bytes (conversion result + NUL). 507/// @param[in] c Character to convert. NUL is assumed to be NL according to 508/// `:h NL-used-for-NUL`. 509void transchar_nonprint (const buf_T * buf ,char * charbuf ,int c ) 510{ 511if (c == NL ) { 512// we use newline in place of a NUL 513c = NUL ; 514 }else if (buf != NULL && c == CAR && get_fileformat (buf )== EOL_MAC ) { 515// we use CR in place of NL in this case 516c = NL ; 517 } 518assert (c <=0xff ); 519 520if (dy_flags & kOptDyFlagUhex || c > 0x7f ) { 521// 'display' has "uhex" 522transchar_hex (charbuf ,c ); 523 }else { 524// 0x00 - 0x1f and 0x7f 525charbuf [0 ]= '^' ; 526// DEL displayed as ^? 527charbuf [1 ]= (char )(uint8_t )(c ^0x40 ); 528 529charbuf [2 ]= NUL ; 530 } 531} 532 533/// Convert a non-printable character to hex C string like "<FFFF>" 534/// 535/// @param[out] buf Buffer to store result in. 536/// @param[in] c Character to convert. 537/// 538/// @return Number of bytes stored in buffer, excluding trailing NUL byte. 539size_t transchar_hex (char * const buf ,const int c ) 540FUNC_ATTR_NONNULL_ALL 541{ 542size_t i = 0 ; 543 544buf [i ++ ]= '<' ; 545if (c > 0xFF ) { 546if (c > 0xFFFF ) { 547buf [i ++ ]= (char )nr2hex ((unsigned )c >>20 ); 548buf [i ++ ]= (char )nr2hex ((unsigned )c >>16 ); 549 } 550buf [i ++ ]= (char )nr2hex ((unsigned )c >>12 ); 551buf [i ++ ]= (char )nr2hex ((unsigned )c >>8 ); 552 } 553buf [i ++ ]= (char )(nr2hex ((unsigned )c >>4 )); 554buf [i ++ ]= (char )(nr2hex ((unsigned )c )); 555buf [i ++ ]= '>' ; 556buf [i ]= NUL ; 557return i ; 558} 559 560/// Mirror text "str" for right-left displaying. 561/// Only works for single-byte characters (e.g., numbers). 562void rl_mirror_ascii (char * str ,char * end ) 563{ 564for (char * p1 = str ,* p2 = (end ?end :str + strlen (str ))- 1 ;p1 < p2 ;p1 ++ ,p2 -- ) { 565char t = * p1 ; 566* p1 = * p2 ; 567* p2 = t ; 568 } 569} 570 571/// Convert the lower 4 bits of byte "c" to its hex character 572/// 573/// Lower case letters are used to avoid the confusion of <F1> being 0xf1 or 574/// function key 1. 575/// 576/// @param[in] n Number to convert. 577/// 578/// @return the hex character. 579static inline unsigned nr2hex (unsigned n ) 580FUNC_ATTR_CONST FUNC_ATTR_WARN_UNUSED_RESULT 581{ 582if ((n & 0xf ) <=9 ) { 583return (n & 0xf )+ '0' ; 584 } 585return (n & 0xf )- 10 + 'a' ; 586} 587 588/// Return number of display cells occupied by character "c". 589/// 590/// "c" can be a special key (negative number) in which case 3 or 4 is returned. 591/// A TAB is counted as two cells: "^I" or four: "<09>". 592/// 593/// @param c 594/// 595/// @return Number of display cells. 596int char2cells (int c ) 597{ 598if (c >=0x80 ) { 599// UTF-8: above 0x80 need to check the value 600return utf_char2cells (c ); 601 } 602return ascii2cells (c ); 603} 604 605/// Return the number of character cells string "s" will take on the screen, 606/// counting TABs as two characters: "^I". 607/// 608/// 's' must be non-null. 609/// 610/// @param s 611/// 612/// @return number of character cells. 613int vim_strsize (const char * s ) 614{ 615return vim_strnsize (s ,MAXCOL ); 616} 617 618/// Return the number of character cells string "s[len]" will take on the 619/// screen, counting TABs as two characters: "^I". 620/// 621/// 's' must be non-null. 622/// 623/// @param s 624/// @param len 625/// 626/// @return Number of character cells. 627int vim_strnsize (const char * s ,int len ) 628{ 629assert (s != NULL ); 630int size = 0 ; 631while (* s != NUL && -- len >=0 ) { 632int l = utfc_ptr2len (s ); 633size += ptr2cells (s ); 634s += l ; 635len -= l - 1 ; 636 } 637return size ; 638} 639 640/// Check that "c" is a normal identifier character: 641/// Letters and characters from the 'isident' option. 642/// 643/// @param c character to check 644bool vim_isIDc (int c ) 645FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT 646{ 647return c > 0 && c < 0x100 && GET_CHARTAB (isi_chartab ,c ); 648} 649 650/// Check that "c" is a keyword character: 651/// Letters and characters from 'iskeyword' option for the current buffer. 652/// For multi-byte characters mb_get_class() is used (builtin rules). 653/// 654/// @param c character to check 655bool vim_iswordc (const int c ) 656FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT 657{ 658return vim_iswordc_buf (c ,curbuf ); 659} 660 661/// Check that "c" is a keyword character 662/// Letters and characters from 'iskeyword' option for given buffer. 663/// For multi-byte characters mb_get_class() is used (builtin rules). 664/// 665/// @param[in] c Character to check. 666/// @param[in] chartab Buffer chartab. 667bool vim_iswordc_tab (const int c ,const uint64_t * const chartab ) 668FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 669{ 670return (c >=0x100 671 ? (utf_class_tab (c ,chartab ) >=2 ) 672 : (c > 0 && GET_CHARTAB (chartab ,c )!= 0 )); 673} 674 675/// Check that "c" is a keyword character: 676/// Letters and characters from 'iskeyword' option for given buffer. 677/// For multi-byte characters mb_get_class() is used (builtin rules). 678/// 679/// @param c character to check 680/// @param buf buffer whose keywords to use 681bool vim_iswordc_buf (const int c ,buf_T * const buf ) 682FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG (2 ) 683{ 684return vim_iswordc_tab (c ,buf -> b_chartab ); 685} 686 687/// Just like vim_iswordc() but uses a pointer to the (multi-byte) character. 688/// 689/// @param p pointer to the multi-byte character 690/// 691/// @return true if "p" points to a keyword character. 692bool vim_iswordp (const char * const p ) 693FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 694{ 695return vim_iswordp_buf (p ,curbuf ); 696} 697 698/// Just like vim_iswordc_buf() but uses a pointer to the (multi-byte) 699/// character. 700/// 701/// @param p pointer to the multi-byte character 702/// @param buf buffer whose keywords to use 703/// 704/// @return true if "p" points to a keyword character. 705bool vim_iswordp_buf (const char * const p ,buf_T * const buf ) 706FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 707{ 708int c = (uint8_t )(* p ); 709 710if (MB_BYTE2LEN (c )> 1 ) { 711c = utf_ptr2char (p ); 712 } 713return vim_iswordc_buf (c ,buf ); 714} 715 716/// Check that "c" is a valid file-name character as specified with the 717/// 'isfname' option. 718/// Assume characters above 0x100 are valid (multi-byte). 719/// To be used for commands like "gf". 720/// 721/// @param c character to check 722bool vim_isfilec (int c ) 723FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT 724{ 725return c >=0x100 || (c > 0 && GET_CHARTAB (isf_chartab ,c )); 726} 727 728/// Check if "c" is a valid file-name character, including characters left 729/// out of 'isfname' to make "gf" work, such as ',', ' ', '@', ':', etc. 730bool vim_is_fname_char (int c ) 731FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT 732{ 733return vim_isfilec (c )|| c == ',' || c == ' ' || c == '@' || c == ':' ; 734} 735 736/// Check that "c" is a valid file-name character or a wildcard character 737/// Assume characters above 0x100 are valid (multi-byte). 738/// Explicitly interpret ']' as a wildcard character as path_has_wildcard("]") 739/// returns false. 740/// 741/// @param c character to check 742bool vim_isfilec_or_wc (int c ) 743FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT 744{ 745char buf [2 ]; 746buf [0 ]= (char )c ; 747buf [1 ]= NUL ; 748return vim_isfilec (c )|| c == ']' || path_has_wildcard (buf , true); 749} 750 751/// Check that "c" is a printable character. 752/// 753/// @param c character to check 754bool vim_isprintc (int c ) 755FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT 756{ 757if (c >=0x100 ) { 758return utf_printable (c ); 759 } 760return c > 0 && ((c & 0x60 )!= 0 && c != 0x7f ); 761} 762 763/// skipwhite: skip over ' ' and '\t'. 764/// 765/// @param[in] p String to skip in. 766/// 767/// @return Pointer to character after the skipped whitespace. 768char * skipwhite (const char * p ) 769FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 770FUNC_ATTR_NONNULL_RET 771{ 772while (ascii_iswhite (* p )) { 773p ++ ; 774 } 775return (char * )p ; 776} 777 778/// Like `skipwhite`, but skip up to `len` characters. 779/// @see skipwhite 780/// 781/// @param[in] p String to skip in. 782/// @param[in] len Max length to skip. 783/// 784/// @return Pointer to character after the skipped whitespace, or the `len`-th 785/// character in the string. 786char * skipwhite_len (const char * p ,size_t len ) 787FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 788FUNC_ATTR_NONNULL_RET 789{ 790for (;len > 0 && ascii_iswhite (* p );len -- ) { 791p ++ ; 792 } 793return (char * )p ; 794} 795 796// getwhitecols: return the number of whitespace 797// columns (bytes) at the start of a given line 798intptr_t getwhitecols_curline (void ) 799{ 800return getwhitecols (get_cursor_line_ptr ()); 801} 802 803intptr_t getwhitecols (const char * p ) 804FUNC_ATTR_PURE 805{ 806return skipwhite (p )- p ; 807} 808 809/// Skip over digits 810/// 811/// @param[in] q String to skip digits in. 812/// 813/// @return Pointer to the character after the skipped digits. 814char * skipdigits (const char * q ) 815FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 816FUNC_ATTR_NONNULL_RET 817{ 818const char * p = q ; 819while (ascii_isdigit (* p )) { 820// skip to next non-digit 821p ++ ; 822 } 823return (char * )p ; 824} 825 826/// skip over binary digits 827/// 828/// @param q pointer to string 829/// 830/// @return Pointer to the character after the skipped digits. 831const char * skipbin (const char * q ) 832FUNC_ATTR_PURE 833FUNC_ATTR_NONNULL_ALL 834FUNC_ATTR_NONNULL_RET 835{ 836const char * p = q ; 837while (ascii_isbdigit (* p )) { 838// skip to next non-digit 839p ++ ; 840 } 841return p ; 842} 843 844/// skip over digits and hex characters 845/// 846/// @param q 847/// 848/// @return Pointer to the character after the skipped digits and hex 849/// characters. 850char * skiphex (char * q ) 851FUNC_ATTR_PURE 852{ 853char * p = q ; 854while (ascii_isxdigit (* p )) { 855// skip to next non-digit 856p ++ ; 857 } 858return p ; 859} 860 861/// skip to digit (or NUL after the string) 862/// 863/// @param q 864/// 865/// @return Pointer to the digit or (NUL after the string). 866char * skiptodigit (char * q ) 867FUNC_ATTR_PURE 868{ 869char * p = q ; 870while (* p != NUL && !ascii_isdigit (* p )) { 871// skip to next digit 872p ++ ; 873 } 874return p ; 875} 876 877/// skip to binary character (or NUL after the string) 878/// 879/// @param q pointer to string 880/// 881/// @return Pointer to the binary character or (NUL after the string). 882const char * skiptobin (const char * q ) 883FUNC_ATTR_PURE 884FUNC_ATTR_NONNULL_ALL 885FUNC_ATTR_NONNULL_RET 886{ 887const char * p = q ; 888while (* p != NUL && !ascii_isbdigit (* p )) { 889// skip to next digit 890p ++ ; 891 } 892return p ; 893} 894 895/// skip to hex character (or NUL after the string) 896/// 897/// @param q 898/// 899/// @return Pointer to the hex character or (NUL after the string). 900char * skiptohex (char * q ) 901FUNC_ATTR_PURE 902{ 903char * p = q ; 904while (* p != NUL && !ascii_isxdigit (* p )) { 905// skip to next digit 906p ++ ; 907 } 908return p ; 909} 910 911/// Skip over text until ' ' or '\t' or NUL 912/// 913/// @param[in] p Text to skip over. 914/// 915/// @return Pointer to the next whitespace or NUL character. 916char * skiptowhite (const char * p ) 917FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE 918{ 919while (* p != ' ' && * p != '\t' && * p != NUL ) { 920p ++ ; 921 } 922return (char * )p ; 923} 924 925/// skiptowhite_esc: Like skiptowhite(), but also skip escaped chars 926/// 927/// @param p 928/// 929/// @return Pointer to the next whitespace character. 930char * skiptowhite_esc (const char * p ) 931FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE 932{ 933while (* p != ' ' && * p != '\t' && * p != NUL ) { 934if (((* p == '\\' )|| (* p == Ctrl_V ))&& (* (p + 1 )!= NUL )) { 935p ++ ; 936 } 937p ++ ; 938 } 939return (char * )p ; 940} 941 942/// Skip over text until '\n' or NUL. 943/// 944/// @param[in] p Text to skip over. 945/// 946/// @return Pointer to the next '\n' or NUL character. 947char * skip_to_newline (const char * const p ) 948FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 949FUNC_ATTR_NONNULL_RET 950{ 951return xstrchrnul (p ,NL ); 952} 953 954/// Gets a number from a string and skips over it, signalling overflow. 955/// 956/// @param[out] pp A pointer to a pointer to char. 957/// It will be advanced past the read number. 958/// @param[out] nr Number read from the string. 959/// 960/// @return true on success, false on error/overflow 961bool try_getdigits (char ** pp ,intmax_t * nr ) 962{ 963errno = 0 ; 964* nr = strtoimax (* pp ,pp ,10 ); 965if (errno == ERANGE && (* nr == INTMAX_MIN || * nr == INTMAX_MAX )) { 966return false; 967 } 968return true; 969} 970 971/// Gets a number from a string and skips over it. 972/// 973/// @param[out] pp Pointer to a pointer to char. 974/// It will be advanced past the read number. 975/// @param strict Abort on overflow. 976/// @param def Default value, if parsing fails or overflow occurs. 977/// 978/// @return Number read from the string, or `def` on parse failure or overflow. 979intmax_t getdigits (char ** pp ,bool strict ,intmax_t def ) 980{ 981intmax_t number ; 982int ok = try_getdigits (pp ,& number ); 983if (strict && !ok ) { 984abort (); 985 } 986return ok ?number :def ; 987} 988 989/// Gets an int number from a string. 990/// 991/// @see getdigits 992int getdigits_int (char ** pp ,bool strict ,int def ) 993{ 994intmax_t number = getdigits (pp ,strict ,def ); 995#if SIZEOF_INTMAX_T > SIZEOF_INT 996if (strict ) { 997assert (number >=INT_MIN && number <=INT_MAX ); 998 }else if (!(number >=INT_MIN && number <=INT_MAX )) { 999return def ; 1000 } 1001#endif 1002return (int )number ; 1003} 1004 1005/// Gets a long number from a string. 1006/// 1007/// @see getdigits 1008long getdigits_long (char ** pp ,bool strict ,long def ) 1009{ 1010intmax_t number = getdigits (pp ,strict ,def ); 1011#if SIZEOF_INTMAX_T > SIZEOF_LONG 1012if (strict ) { 1013assert (number >=LONG_MIN && number <=LONG_MAX ); 1014 }else if (!(number >=LONG_MIN && number <=LONG_MAX )) { 1015return def ; 1016 } 1017#endif 1018return (long )number ; 1019} 1020 1021/// Gets a int32_t number from a string. 1022/// 1023/// @see getdigits 1024int32_t getdigits_int32 (char ** pp ,bool strict ,int32_t def ) 1025{ 1026intmax_t number = getdigits (pp ,strict ,def ); 1027#if SIZEOF_INTMAX_T > 4 1028if (strict ) { 1029assert (number >=INT32_MIN && number <=INT32_MAX ); 1030 }else if (!(number >=INT32_MIN && number <=INT32_MAX )) { 1031return def ; 1032 } 1033#endif 1034return (int32_t )number ; 1035} 1036 1037/// Check that "lbuf" is empty or only contains blanks. 1038/// 1039/// @param lbuf line buffer to check 1040bool vim_isblankline (char * lbuf ) 1041FUNC_ATTR_PURE 1042{ 1043char * p = skipwhite (lbuf ); 1044return * p == NUL || * p == '\r' || * p == '\n' ; 1045} 1046 1047/// Convert a string into a long and/or unsigned long, taking care of 1048/// hexadecimal, octal and binary numbers. Accepts a '-' sign. 1049/// If "prep" is not NULL, returns a flag to indicate the type of the number: 1050/// 0 decimal 1051/// '0' octal 1052/// 'O' octal 1053/// 'o' octal 1054/// 'B' bin 1055/// 'b' bin 1056/// 'X' hex 1057/// 'x' hex 1058/// If "len" is not NULL, the length of the number in characters is returned. 1059/// If "nptr" is not NULL, the signed result is returned in it. 1060/// If "unptr" is not NULL, the unsigned result is returned in it. 1061/// If "what" contains STR2NR_BIN recognize binary numbers. 1062/// If "what" contains STR2NR_OCT recognize octal numbers. 1063/// If "what" contains STR2NR_HEX recognize hex numbers. 1064/// If "what" contains STR2NR_FORCE always assume bin/oct/hex. 1065/// If "what" contains STR2NR_QUOTE ignore embedded single quotes 1066/// If maxlen > 0, check at a maximum maxlen chars. 1067/// If strict is true, check the number strictly. return *len = 0 if fail. 1068/// 1069/// @param start 1070/// @param prep Returns guessed type of number 0 = decimal, 'x' or 'X' is 1071/// hexadecimal, '0', 'o' or 'O' is octal, 'b' or 'B' is binary. 1072/// When using STR2NR_FORCE is always zero. 1073/// @param len Returns the detected length of number. 1074/// @param what Recognizes what number passed, @see ChStr2NrFlags. 1075/// @param nptr Returns the signed result. 1076/// @param unptr Returns the unsigned result. 1077/// @param maxlen Max length of string to check. 1078/// @param strict If true, fail if the number has unexpected trailing 1079/// alphanumeric chars: *len is set to 0 and nothing else is 1080/// returned. 1081/// @param overflow When not NULL, set to true for overflow. 1082void vim_str2nr (const char * const start ,int * const prep ,int * const len ,const int what , 1083varnumber_T * const nptr ,uvarnumber_T * const unptr ,const int maxlen , 1084const bool strict ,bool * const overflow ) 1085FUNC_ATTR_NONNULL_ARG (1 ) 1086{ 1087const char * ptr = start ; 1088#define STRING_ENDED (ptr ) \ 1089 (!(maxlen == 0 || (int)((ptr) - start) < maxlen)) 1090int pre = 0 ;// default is decimal 1091const bool negative = (ptr [0 ]== '-' ); 1092uvarnumber_T un = 0 ; 1093 1094if (len != NULL ) { 1095* len = 0 ; 1096 } 1097 1098if (negative ) { 1099ptr ++ ; 1100 } 1101 1102if (what & STR2NR_FORCE ) { 1103// When forcing main consideration is skipping the prefix. Decimal numbers 1104// have no prefixes to skip. pre is not set. 1105switch (what & ~(STR2NR_FORCE |STR2NR_QUOTE )) { 1106case STR2NR_HEX : 1107if (!STRING_ENDED (ptr + 2 ) 1108&& ptr [0 ]== '0' 1109&& (ptr [1 ]== 'x' || ptr [1 ]== 'X' ) 1110&& ascii_isxdigit (ptr [2 ])) { 1111ptr += 2 ; 1112 } 1113 gotovim_str2nr_hex ; 1114case STR2NR_BIN : 1115if (!STRING_ENDED (ptr + 2 ) 1116&& ptr [0 ]== '0' 1117&& (ptr [1 ]== 'b' || ptr [1 ]== 'B' ) 1118&& ascii_isbdigit (ptr [2 ])) { 1119ptr += 2 ; 1120 } 1121 gotovim_str2nr_bin ; 1122// Make STR2NR_OOCT work the same as STR2NR_OCT when forcing. 1123case STR2NR_OCT : 1124case STR2NR_OOCT : 1125case STR2NR_OCT |STR2NR_OOCT : 1126if (!STRING_ENDED (ptr + 2 ) 1127&& ptr [0 ]== '0' 1128&& (ptr [1 ]== 'o' || ptr [1 ]== 'O' ) 1129&& ascii_isodigit (ptr [2 ])) { 1130ptr += 2 ; 1131 } 1132 gotovim_str2nr_oct ; 1133case 0 : 1134 gotovim_str2nr_dec ; 1135default : 1136abort (); 1137 } 1138 }else if ((what & (STR2NR_HEX |STR2NR_OCT |STR2NR_OOCT |STR2NR_BIN )) 1139&& !STRING_ENDED (ptr + 1 )&& ptr [0 ]== '0' && ptr [1 ]!= '8' 1140&& ptr [1 ]!= '9' ) { 1141pre = (uint8_t )ptr [1 ]; 1142// Detect hexadecimal: 0x or 0X followed by hex digit. 1143if ((what & STR2NR_HEX ) 1144&& !STRING_ENDED (ptr + 2 ) 1145&& (pre == 'X' || pre == 'x' ) 1146&& ascii_isxdigit (ptr [2 ])) { 1147ptr += 2 ; 1148 gotovim_str2nr_hex ; 1149 } 1150// Detect binary: 0b or 0B followed by 0 or 1. 1151if ((what & STR2NR_BIN ) 1152&& !STRING_ENDED (ptr + 2 ) 1153&& (pre == 'B' || pre == 'b' ) 1154&& ascii_isbdigit (ptr [2 ])) { 1155ptr += 2 ; 1156 gotovim_str2nr_bin ; 1157 } 1158// Detect octal: 0o or 0O followed by octal digits (without '8' or '9'). 1159if ((what & STR2NR_OOCT ) 1160&& !STRING_ENDED (ptr + 2 ) 1161&& (pre == 'O' || pre == 'o' ) 1162&& ascii_isodigit (ptr [2 ])) { 1163ptr += 2 ; 1164 gotovim_str2nr_oct ; 1165 } 1166// Detect old octal format: 0 followed by octal digits. 1167pre = 0 ; 1168if (!(what & STR2NR_OCT ) 1169|| !ascii_isodigit (ptr [1 ])) { 1170 gotovim_str2nr_dec ; 1171 } 1172for (int i = 2 ; !STRING_ENDED (ptr + i )&& ascii_isdigit (ptr [i ]);i ++ ) { 1173if (ptr [i ]> '7' ) { 1174 gotovim_str2nr_dec ; 1175 } 1176 } 1177pre = '0' ; 1178 gotovim_str2nr_oct ; 1179 }else { 1180 gotovim_str2nr_dec ; 1181 } 1182 1183// Do the conversion manually to avoid sscanf() quirks. 1184abort ();// Should’ve used goto earlier. 1185#define PARSE_NUMBER (base ,cond ,conv ) \ 1186 do { \ 1187 const char *const after_prefix = ptr; \ 1188 while (!STRING_ENDED(ptr)) { \ 1189 if ((what & STR2NR_QUOTE) && ptr > after_prefix && *ptr == '\'') { \ 1190 ptr++; \ 1191 if (!STRING_ENDED(ptr) && (cond)) { \ 1192 continue; \ 1193 } \ 1194 ptr--; \ 1195 } \ 1196 if (!(cond)) { \ 1197 break; \ 1198 } \ 1199 const uvarnumber_T digit = (uvarnumber_T)(conv); \ 1200/* avoid ubsan error for overflow */ \ 1201if (un < UVARNUMBER_MAX / (base ) \ 1202|| (un == UVARNUMBER_MAX / (base ) \ 1203&& ((base )!= 10 || digit <=UVARNUMBER_MAX %10 ))) { \ 1204un = (base )* un + digit ; \ 1205 }else { \ 1206un = UVARNUMBER_MAX ; \ 1207if (overflow != NULL ) { \ 1208* overflow = true; \ 1209 } \ 1210 } \ 1211ptr ++ ; \ 1212 } \ 1213 }while (0 ) 1214vim_str2nr_bin : 1215PARSE_NUMBER (2 , (* ptr == '0' || * ptr == '1' ), (* ptr - '0' )); 1216 gotovim_str2nr_proceed ; 1217vim_str2nr_oct : 1218PARSE_NUMBER (8 , (ascii_isodigit (* ptr )), (* ptr - '0' )); 1219 gotovim_str2nr_proceed ; 1220vim_str2nr_dec : 1221PARSE_NUMBER (10 , (ascii_isdigit (* ptr )), (* ptr - '0' )); 1222 gotovim_str2nr_proceed ; 1223vim_str2nr_hex : 1224PARSE_NUMBER (16 , (ascii_isxdigit (* ptr )), (hex2nr (* ptr ))); 1225 gotovim_str2nr_proceed ; 1226#undef PARSE_NUMBER 1227 1228vim_str2nr_proceed : 1229// Check for an alphanumeric character immediately following, that is 1230// most likely a typo. 1231if (strict && ptr - start != maxlen && ASCII_ISALNUM (* ptr )) { 1232return ; 1233 } 1234 1235if (prep != NULL ) { 1236* prep = pre ; 1237 } 1238 1239if (len != NULL ) { 1240* len = (int )(ptr - start ); 1241 } 1242 1243if (nptr != NULL ) { 1244if (negative ) {// account for leading '-' for decimal numbers 1245// avoid ubsan error for overflow 1246if (un > VARNUMBER_MAX ) { 1247* nptr = VARNUMBER_MIN ; 1248if (overflow != NULL ) { 1249* overflow = true; 1250 } 1251 }else { 1252* nptr = - (varnumber_T )un ; 1253 } 1254 }else { 1255if (un > VARNUMBER_MAX ) { 1256un = VARNUMBER_MAX ; 1257if (overflow != NULL ) { 1258* overflow = true; 1259 } 1260 } 1261* nptr = (varnumber_T )un ; 1262 } 1263 } 1264 1265if (unptr != NULL ) { 1266* unptr = un ; 1267 } 1268#undef STRING_ENDED 1269} 1270 1271/// Return the value of a single hex character. 1272/// Only valid when the argument is '0' - '9', 'A' - 'F' or 'a' - 'f'. 1273/// 1274/// @param c 1275/// 1276/// @return The value of the hex character. 1277int hex2nr (int c ) 1278FUNC_ATTR_CONST 1279{ 1280if ((c >='a' )&& (c <='f' )) { 1281return c - 'a' + 10 ; 1282 } 1283 1284if ((c >='A' )&& (c <='F' )) { 1285return c - 'A' + 10 ; 1286 } 1287return c - '0' ; 1288} 1289 1290/// Convert two hex characters to a byte. 1291/// 1292/// @return -1 if one of the characters is not hex. 1293int hexhex2nr (const char * p ) 1294FUNC_ATTR_PURE 1295{ 1296if (!ascii_isxdigit (p [0 ])|| !ascii_isxdigit (p [1 ])) { 1297return -1 ; 1298 } 1299return (hex2nr (p [0 ]) <<4 )+ hex2nr (p [1 ]); 1300} 1301 1302/// Check that "str" starts with a backslash that should be removed. 1303/// For Windows this is only done when the character after the 1304/// backslash is not a normal file name character. 1305/// '$' is a valid file name character, we don't remove the backslash before 1306/// it. This means it is not possible to use an environment variable after a 1307/// backslash. "C:\$VIM\doc" is taken literally, only "$VIM\doc" works. 1308/// Although "\ name" is valid, the backslash in "Program\ files" must be 1309/// removed. Assume a file name doesn't start with a space. 1310/// For multi-byte names, never remove a backslash before a non-ascii 1311/// character, assume that all multi-byte characters are valid file name 1312/// characters. 1313/// 1314/// @param str file path string to check 1315bool rem_backslash (const char * str ) 1316FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL 1317{ 1318#ifdef BACKSLASH_IN_FILENAME 1319return str [0 ]== '\\' 1320&& (uint8_t )str [1 ]< 0x80 1321&& (str [1 ]== ' ' 1322|| (str [1 ]!= NUL 1323&& str [1 ]!= '*' 1324&& str [1 ]!= '?' 1325&& !vim_isfilec ((uint8_t )str [1 ]))); 1326 1327#else 1328return str [0 ]== '\\' && str [1 ]!= NUL ; 1329#endif 1330} 1331 1332/// Halve the number of backslashes in a file name argument. 1333/// 1334/// @param p 1335void backslash_halve (char * p ) 1336{ 1337for (;* p && !rem_backslash (p );p ++ ) {} 1338if (* p != NUL ) { 1339char * dst = p ; 1340 gotostart ; 1341while (* p != NUL ) { 1342if (rem_backslash (p )) { 1343start : 1344* dst ++ = * (p + 1 ); 1345p += 2 ; 1346 }else { 1347* dst ++ = * p ++ ; 1348 } 1349 } 1350* dst = NUL ; 1351 } 1352} 1353 1354/// backslash_halve() plus save the result in allocated memory. 1355/// 1356/// @param p 1357/// 1358/// @return String with the number of backslashes halved. 1359char * backslash_halve_save (const char * p ) 1360FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET 1361{ 1362char * res = xmalloc (strlen (p )+ 1 ); 1363char * dst = res ; 1364while (* p != NUL ) { 1365if (rem_backslash (p )) { 1366* dst ++ = * (p + 1 ); 1367p += 2 ; 1368 }else { 1369* dst ++ = * p ++ ; 1370 } 1371 } 1372* dst = NUL ; 1373return res ; 1374}