Mercurial > vim
annotate src/strings.c @ 25529:bb1097899693 v8.2.3301
patch 8.2.3301: memory allocation functions don't have their own place
Commit: https://github.com/vim/vim/commit/cbae5802832b29f3a1af4cb6b0fc8cf69f17cbf4
Author: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Fri Aug 6 21:51:55 2021 +0200
patch 8.2.3301: memory allocation functions don't have their own place
Problem: Memory allocation functions don't have their own place.
Solution: Move memory allocation functions to alloc.c. (Yegappan
Lakshmanan, closes #8717)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Fri, 06 Aug 2021 22:00:04 +0200 |
parents | e8e2c4d33b9b |
children | 0082503ff2ff |
rev | line source |
---|---|
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1 /* vi:set ts=8 sts=4 sw=4 noet: |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
2 * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
3 * VIM - Vi IMproved by Bram Moolenaar |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
4 * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
5 * Do ":help uganda" in Vim to read copying and usage conditions. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
6 * Do ":help credits" in Vim to see a list of people who contributed. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
7 * See README.txt for an overview of the Vim source code. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
8 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
9 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
10 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
11 * strings.c: string manipulation functions |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
12 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
13 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
14 #include "vim.h" |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
15 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
16 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
17 * Copy "string" into newly allocated memory. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
18 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
19 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
20 vim_strsave(char_u *string) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
21 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
22 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
23 size_t len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
24 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
25 len = STRLEN(string) + 1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
26 p = alloc(len); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
27 if (p != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
28 mch_memmove(p, string, len); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
29 return p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
30 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
31 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
32 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
33 * Copy up to "len" bytes of "string" into newly allocated memory and |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
34 * terminate with a NUL. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
35 * The allocated memory always has size "len + 1", also when "string" is |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
36 * shorter. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
37 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
38 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
39 vim_strnsave(char_u *string, size_t len) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
40 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
41 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
42 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
43 p = alloc(len + 1); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
44 if (p != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
45 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
46 STRNCPY(p, string, len); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
47 p[len] = NUL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
48 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
49 return p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
50 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
51 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
52 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
53 * Same as vim_strsave(), but any characters found in esc_chars are preceded |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
54 * by a backslash. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
55 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
56 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
57 vim_strsave_escaped(char_u *string, char_u *esc_chars) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
58 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
59 return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
60 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
61 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
62 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
63 * Same as vim_strsave_escaped(), but when "bsl" is TRUE also escape |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
64 * characters where rem_backslash() would remove the backslash. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
65 * Escape the characters with "cc". |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
66 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
67 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
68 vim_strsave_escaped_ext( |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
69 char_u *string, |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
70 char_u *esc_chars, |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
71 int cc, |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
72 int bsl) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
73 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
74 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
75 char_u *p2; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
76 char_u *escaped_string; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
77 unsigned length; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
78 int l; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
79 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
80 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
81 * First count the number of backslashes required. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
82 * Then allocate the memory and insert them. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
83 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
84 length = 1; // count the trailing NUL |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
85 for (p = string; *p; p++) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
86 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
87 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
88 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
89 length += l; // count a multibyte char |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
90 p += l - 1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
91 continue; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
92 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
93 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p))) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
94 ++length; // count a backslash |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
95 ++length; // count an ordinary char |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
96 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
97 escaped_string = alloc(length); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
98 if (escaped_string != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
99 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
100 p2 = escaped_string; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
101 for (p = string; *p; p++) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
102 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
103 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
104 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
105 mch_memmove(p2, p, (size_t)l); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
106 p2 += l; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
107 p += l - 1; // skip multibyte char |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
108 continue; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
109 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
110 if (vim_strchr(esc_chars, *p) != NULL || (bsl && rem_backslash(p))) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
111 *p2++ = cc; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
112 *p2++ = *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
113 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
114 *p2 = NUL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
115 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
116 return escaped_string; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
117 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
118 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
119 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
120 * Return TRUE when 'shell' has "csh" in the tail. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
121 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
122 int |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
123 csh_like_shell(void) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
124 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
125 return (strstr((char *)gettail(p_sh), "csh") != NULL); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
126 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
127 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
128 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
129 * Escape "string" for use as a shell argument with system(). |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
130 * This uses single quotes, except when we know we need to use double quotes |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
131 * (MS-DOS and MS-Windows not using PowerShell and without 'shellslash' set). |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
132 * PowerShell also uses a novel escaping for enclosed single quotes - double |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
133 * them up. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
134 * Escape a newline, depending on the 'shell' option. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
135 * When "do_special" is TRUE also replace "!", "%", "#" and things starting |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
136 * with "<" like "<cfile>". |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
137 * When "do_newline" is FALSE do not escape newline unless it is csh shell. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
138 * Returns the result in allocated memory, NULL if we have run out. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
139 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
140 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
141 vim_strsave_shellescape(char_u *string, int do_special, int do_newline) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
142 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
143 unsigned length; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
144 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
145 char_u *d; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
146 char_u *escaped_string; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
147 int l; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
148 int csh_like; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
149 char_u *shname; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
150 int powershell; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
151 # ifdef MSWIN |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
152 int double_quotes; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
153 # endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
154 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
155 // Only csh and similar shells expand '!' within single quotes. For sh and |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
156 // the like we must not put a backslash before it, it will be taken |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
157 // literally. If do_special is set the '!' will be escaped twice. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
158 // Csh also needs to have "\n" escaped twice when do_special is set. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
159 csh_like = csh_like_shell(); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
160 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
161 // PowerShell uses it's own version for quoting single quotes |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
162 shname = gettail(p_sh); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
163 powershell = strstr((char *)shname, "pwsh") != NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
164 # ifdef MSWIN |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
165 powershell = powershell || strstr((char *)shname, "powershell") != NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
166 // PowerShell only accepts single quotes so override shellslash. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
167 double_quotes = !powershell && !p_ssl; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
168 # endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
169 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
170 // First count the number of extra bytes required. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
171 length = (unsigned)STRLEN(string) + 3; // two quotes and a trailing NUL |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
172 for (p = string; *p != NUL; MB_PTR_ADV(p)) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
173 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
174 # ifdef MSWIN |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
175 if (double_quotes) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
176 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
177 if (*p == '"') |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
178 ++length; // " -> "" |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
179 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
180 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
181 # endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
182 if (*p == '\'') |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
183 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
184 if (powershell) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
185 length +=2; // ' => '' |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
186 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
187 length += 3; // ' => '\'' |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
188 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
189 if ((*p == '\n' && (csh_like || do_newline)) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
190 || (*p == '!' && (csh_like || do_special))) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
191 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
192 ++length; // insert backslash |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
193 if (csh_like && do_special) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
194 ++length; // insert backslash |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
195 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
196 if (do_special && find_cmdline_var(p, &l) >= 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
197 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
198 ++length; // insert backslash |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
199 p += l - 1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
200 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
201 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
202 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
203 // Allocate memory for the result and fill it. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
204 escaped_string = alloc(length); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
205 if (escaped_string != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
206 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
207 d = escaped_string; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
208 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
209 // add opening quote |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
210 # ifdef MSWIN |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
211 if (double_quotes) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
212 *d++ = '"'; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
213 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
214 # endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
215 *d++ = '\''; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
216 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
217 for (p = string; *p != NUL; ) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
218 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
219 # ifdef MSWIN |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
220 if (double_quotes) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
221 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
222 if (*p == '"') |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
223 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
224 *d++ = '"'; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
225 *d++ = '"'; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
226 ++p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
227 continue; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
228 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
229 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
230 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
231 # endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
232 if (*p == '\'') |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
233 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
234 if (powershell) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
235 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
236 *d++ = '\''; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
237 *d++ = '\''; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
238 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
239 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
240 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
241 *d++ = '\''; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
242 *d++ = '\\'; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
243 *d++ = '\''; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
244 *d++ = '\''; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
245 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
246 ++p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
247 continue; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
248 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
249 if ((*p == '\n' && (csh_like || do_newline)) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
250 || (*p == '!' && (csh_like || do_special))) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
251 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
252 *d++ = '\\'; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
253 if (csh_like && do_special) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
254 *d++ = '\\'; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
255 *d++ = *p++; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
256 continue; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
257 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
258 if (do_special && find_cmdline_var(p, &l) >= 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
259 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
260 *d++ = '\\'; // insert backslash |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
261 while (--l >= 0) // copy the var |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
262 *d++ = *p++; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
263 continue; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
264 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
265 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
266 MB_COPY_CHAR(p, d); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
267 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
268 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
269 // add terminating quote and finish with a NUL |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
270 # ifdef MSWIN |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
271 if (double_quotes) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
272 *d++ = '"'; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
273 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
274 # endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
275 *d++ = '\''; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
276 *d = NUL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
277 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
278 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
279 return escaped_string; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
280 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
281 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
282 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
283 * Like vim_strsave(), but make all characters uppercase. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
284 * This uses ASCII lower-to-upper case translation, language independent. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
285 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
286 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
287 vim_strsave_up(char_u *string) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
288 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
289 char_u *p1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
290 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
291 p1 = vim_strsave(string); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
292 vim_strup(p1); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
293 return p1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
294 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
295 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
296 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
297 * Like vim_strnsave(), but make all characters uppercase. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
298 * This uses ASCII lower-to-upper case translation, language independent. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
299 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
300 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
301 vim_strnsave_up(char_u *string, size_t len) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
302 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
303 char_u *p1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
304 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
305 p1 = vim_strnsave(string, len); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
306 vim_strup(p1); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
307 return p1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
308 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
309 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
310 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
311 * ASCII lower-to-upper case translation, language independent. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
312 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
313 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
314 vim_strup( |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
315 char_u *p) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
316 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
317 char_u *p2; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
318 int c; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
319 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
320 if (p != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
321 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
322 p2 = p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
323 while ((c = *p2) != NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
324 #ifdef EBCDIC |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
325 *p2++ = isalpha(c) ? toupper(c) : c; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
326 #else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
327 *p2++ = (c < 'a' || c > 'z') ? c : (c - 0x20); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
328 #endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
329 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
330 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
331 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
332 #if defined(FEAT_EVAL) || defined(FEAT_SPELL) || defined(PROTO) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
333 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
334 * Make string "s" all upper-case and return it in allocated memory. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
335 * Handles multi-byte characters as well as possible. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
336 * Returns NULL when out of memory. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
337 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
338 static char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
339 strup_save(char_u *orig) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
340 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
341 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
342 char_u *res; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
343 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
344 res = p = vim_strsave(orig); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
345 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
346 if (res != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
347 while (*p != NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
348 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
349 int l; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
350 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
351 if (enc_utf8) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
352 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
353 int c, uc; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
354 int newl; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
355 char_u *s; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
356 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
357 c = utf_ptr2char(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
358 l = utf_ptr2len(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
359 if (c == 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
360 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
361 // overlong sequence, use only the first byte |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
362 c = *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
363 l = 1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
364 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
365 uc = utf_toupper(c); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
366 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
367 // Reallocate string when byte count changes. This is rare, |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
368 // thus it's OK to do another malloc()/free(). |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
369 newl = utf_char2len(uc); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
370 if (newl != l) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
371 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
372 s = alloc(STRLEN(res) + 1 + newl - l); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
373 if (s == NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
374 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
375 vim_free(res); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
376 return NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
377 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
378 mch_memmove(s, res, p - res); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
379 STRCPY(s + (p - res) + newl, p + l); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
380 p = s + (p - res); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
381 vim_free(res); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
382 res = s; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
383 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
384 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
385 utf_char2bytes(uc, p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
386 p += newl; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
387 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
388 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
389 p += l; // skip multi-byte character |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
390 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
391 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
392 *p = TOUPPER_LOC(*p); // note that toupper() can be a macro |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
393 p++; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
394 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
395 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
396 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
397 return res; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
398 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
399 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
400 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
401 * Make string "s" all lower-case and return it in allocated memory. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
402 * Handles multi-byte characters as well as possible. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
403 * Returns NULL when out of memory. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
404 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
405 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
406 strlow_save(char_u *orig) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
407 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
408 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
409 char_u *res; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
410 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
411 res = p = vim_strsave(orig); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
412 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
413 if (res != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
414 while (*p != NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
415 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
416 int l; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
417 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
418 if (enc_utf8) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
419 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
420 int c, lc; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
421 int newl; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
422 char_u *s; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
423 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
424 c = utf_ptr2char(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
425 l = utf_ptr2len(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
426 if (c == 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
427 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
428 // overlong sequence, use only the first byte |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
429 c = *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
430 l = 1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
431 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
432 lc = utf_tolower(c); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
433 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
434 // Reallocate string when byte count changes. This is rare, |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
435 // thus it's OK to do another malloc()/free(). |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
436 newl = utf_char2len(lc); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
437 if (newl != l) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
438 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
439 s = alloc(STRLEN(res) + 1 + newl - l); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
440 if (s == NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
441 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
442 vim_free(res); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
443 return NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
444 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
445 mch_memmove(s, res, p - res); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
446 STRCPY(s + (p - res) + newl, p + l); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
447 p = s + (p - res); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
448 vim_free(res); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
449 res = s; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
450 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
451 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
452 utf_char2bytes(lc, p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
453 p += newl; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
454 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
455 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
456 p += l; // skip multi-byte character |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
457 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
458 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
459 *p = TOLOWER_LOC(*p); // note that tolower() can be a macro |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
460 p++; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
461 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
462 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
463 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
464 return res; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
465 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
466 #endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
467 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
468 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
469 * delete spaces at the end of a string |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
470 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
471 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
472 del_trailing_spaces(char_u *ptr) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
473 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
474 char_u *q; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
475 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
476 q = ptr + STRLEN(ptr); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
477 while (--q > ptr && VIM_ISWHITE(q[0]) && q[-1] != '\\' && q[-1] != Ctrl_V) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
478 *q = NUL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
479 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
480 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
481 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
482 * Like strncpy(), but always terminate the result with one NUL. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
483 * "to" must be "len + 1" long! |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
484 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
485 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
486 vim_strncpy(char_u *to, char_u *from, size_t len) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
487 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
488 STRNCPY(to, from, len); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
489 to[len] = NUL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
490 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
491 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
492 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
493 * Like strcat(), but make sure the result fits in "tosize" bytes and is |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
494 * always NUL terminated. "from" and "to" may overlap. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
495 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
496 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
497 vim_strcat(char_u *to, char_u *from, size_t tosize) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
498 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
499 size_t tolen = STRLEN(to); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
500 size_t fromlen = STRLEN(from); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
501 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
502 if (tolen + fromlen + 1 > tosize) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
503 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
504 mch_memmove(to + tolen, from, tosize - tolen - 1); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
505 to[tosize - 1] = NUL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
506 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
507 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
508 mch_memmove(to + tolen, from, fromlen + 1); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
509 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
510 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
511 #if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
512 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
513 * Compare two strings, ignoring case, using current locale. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
514 * Doesn't work for multi-byte characters. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
515 * return 0 for match, < 0 for smaller, > 0 for bigger |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
516 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
517 int |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
518 vim_stricmp(char *s1, char *s2) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
519 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
520 int i; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
521 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
522 for (;;) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
523 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
524 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
525 if (i != 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
526 return i; // this character different |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
527 if (*s1 == NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
528 break; // strings match until NUL |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
529 ++s1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
530 ++s2; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
531 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
532 return 0; // strings match |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
533 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
534 #endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
535 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
536 #if (!defined(HAVE_STRNCASECMP) && !defined(HAVE_STRNICMP)) || defined(PROTO) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
537 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
538 * Compare two strings, for length "len", ignoring case, using current locale. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
539 * Doesn't work for multi-byte characters. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
540 * return 0 for match, < 0 for smaller, > 0 for bigger |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
541 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
542 int |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
543 vim_strnicmp(char *s1, char *s2, size_t len) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
544 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
545 int i; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
546 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
547 while (len > 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
548 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
549 i = (int)TOLOWER_LOC(*s1) - (int)TOLOWER_LOC(*s2); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
550 if (i != 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
551 return i; // this character different |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
552 if (*s1 == NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
553 break; // strings match until NUL |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
554 ++s1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
555 ++s2; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
556 --len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
557 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
558 return 0; // strings match |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
559 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
560 #endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
561 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
562 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
563 * Search for first occurrence of "c" in "string". |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
564 * Version of strchr() that handles unsigned char strings with characters from |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
565 * 128 to 255 correctly. It also doesn't return a pointer to the NUL at the |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
566 * end of the string. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
567 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
568 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
569 vim_strchr(char_u *string, int c) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
570 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
571 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
572 int b; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
573 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
574 p = string; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
575 if (enc_utf8 && c >= 0x80) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
576 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
577 while (*p != NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
578 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
579 int l = utfc_ptr2len(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
580 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
581 // Avoid matching an illegal byte here. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
582 if (utf_ptr2char(p) == c && l > 1) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
583 return p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
584 p += l; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
585 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
586 return NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
587 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
588 if (enc_dbcs != 0 && c > 255) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
589 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
590 int n2 = c & 0xff; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
591 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
592 c = ((unsigned)c >> 8) & 0xff; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
593 while ((b = *p) != NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
594 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
595 if (b == c && p[1] == n2) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
596 return p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
597 p += (*mb_ptr2len)(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
598 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
599 return NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
600 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
601 if (has_mbyte) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
602 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
603 while ((b = *p) != NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
604 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
605 if (b == c) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
606 return p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
607 p += (*mb_ptr2len)(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
608 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
609 return NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
610 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
611 while ((b = *p) != NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
612 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
613 if (b == c) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
614 return p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
615 ++p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
616 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
617 return NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
618 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
619 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
620 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
621 * Version of strchr() that only works for bytes and handles unsigned char |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
622 * strings with characters above 128 correctly. It also doesn't return a |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
623 * pointer to the NUL at the end of the string. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
624 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
625 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
626 vim_strbyte(char_u *string, int c) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
627 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
628 char_u *p = string; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
629 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
630 while (*p != NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
631 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
632 if (*p == c) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
633 return p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
634 ++p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
635 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
636 return NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
637 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
638 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
639 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
640 * Search for last occurrence of "c" in "string". |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
641 * Version of strrchr() that handles unsigned char strings with characters from |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
642 * 128 to 255 correctly. It also doesn't return a pointer to the NUL at the |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
643 * end of the string. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
644 * Return NULL if not found. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
645 * Does not handle multi-byte char for "c"! |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
646 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
647 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
648 vim_strrchr(char_u *string, int c) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
649 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
650 char_u *retval = NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
651 char_u *p = string; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
652 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
653 while (*p) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
654 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
655 if (*p == c) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
656 retval = p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
657 MB_PTR_ADV(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
658 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
659 return retval; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
660 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
661 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
662 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
663 * Vim's version of strpbrk(), in case it's missing. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
664 * Don't generate a prototype for this, causes problems when it's not used. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
665 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
666 #ifndef PROTO |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
667 # ifndef HAVE_STRPBRK |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
668 # ifdef vim_strpbrk |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
669 # undef vim_strpbrk |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
670 # endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
671 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
672 vim_strpbrk(char_u *s, char_u *charset) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
673 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
674 while (*s) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
675 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
676 if (vim_strchr(charset, *s) != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
677 return s; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
678 MB_PTR_ADV(s); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
679 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
680 return NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
681 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
682 # endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
683 #endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
684 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
685 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
686 * Sort an array of strings. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
687 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
688 static int sort_compare(const void *s1, const void *s2); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
689 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
690 static int |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
691 sort_compare(const void *s1, const void *s2) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
692 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
693 return STRCMP(*(char **)s1, *(char **)s2); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
694 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
695 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
696 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
697 sort_strings( |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
698 char_u **files, |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
699 int count) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
700 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
701 qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
702 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
703 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
704 #if defined(FEAT_QUICKFIX) || defined(FEAT_SPELL) || defined(PROTO) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
705 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
706 * Return TRUE if string "s" contains a non-ASCII character (128 or higher). |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
707 * When "s" is NULL FALSE is returned. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
708 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
709 int |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
710 has_non_ascii(char_u *s) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
711 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
712 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
713 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
714 if (s != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
715 for (p = s; *p != NUL; ++p) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
716 if (*p >= 128) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
717 return TRUE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
718 return FALSE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
719 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
720 #endif |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
721 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
722 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
723 * Concatenate two strings and return the result in allocated memory. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
724 * Returns NULL when out of memory. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
725 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
726 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
727 concat_str(char_u *str1, char_u *str2) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
728 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
729 char_u *dest; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
730 size_t l = str1 == NULL ? 0 : STRLEN(str1); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
731 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
732 dest = alloc(l + (str2 == NULL ? 0 : STRLEN(str2)) + 1L); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
733 if (dest != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
734 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
735 if (str1 == NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
736 *dest = NUL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
737 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
738 STRCPY(dest, str1); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
739 if (str2 != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
740 STRCPY(dest + l, str2); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
741 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
742 return dest; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
743 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
744 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
745 #if defined(FEAT_EVAL) || defined(PROTO) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
746 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
747 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
748 * Return string "str" in ' quotes, doubling ' characters. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
749 * If "str" is NULL an empty string is assumed. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
750 * If "function" is TRUE make it function('string'). |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
751 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
752 char_u * |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
753 string_quote(char_u *str, int function) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
754 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
755 unsigned len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
756 char_u *p, *r, *s; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
757 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
758 len = (function ? 13 : 3); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
759 if (str != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
760 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
761 len += (unsigned)STRLEN(str); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
762 for (p = str; *p != NUL; MB_PTR_ADV(p)) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
763 if (*p == '\'') |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
764 ++len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
765 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
766 s = r = alloc(len); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
767 if (r != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
768 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
769 if (function) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
770 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
771 STRCPY(r, "function('"); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
772 r += 10; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
773 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
774 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
775 *r++ = '\''; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
776 if (str != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
777 for (p = str; *p != NUL; ) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
778 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
779 if (*p == '\'') |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
780 *r++ = '\''; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
781 MB_COPY_CHAR(p, r); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
782 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
783 *r++ = '\''; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
784 if (function) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
785 *r++ = ')'; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
786 *r++ = NUL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
787 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
788 return s; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
789 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
790 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
791 static void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
792 byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
793 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
794 char_u *t; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
795 char_u *str; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
796 varnumber_T idx; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
797 |
25252
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
798 rettv->vval.v_number = -1; |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
799 |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
800 if (in_vim9script() |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
801 && (check_for_string_arg(argvars, 0) == FAIL |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
802 || check_for_number_arg(argvars, 1) == FAIL)) |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
803 return; |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
804 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
805 str = tv_get_string_chk(&argvars[0]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
806 idx = tv_get_number_chk(&argvars[1], NULL); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
807 if (str == NULL || idx < 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
808 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
809 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
810 t = str; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
811 for ( ; idx > 0; idx--) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
812 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
813 if (*t == NUL) // EOL reached |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
814 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
815 if (enc_utf8 && comp) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
816 t += utf_ptr2len(t); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
817 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
818 t += (*mb_ptr2len)(t); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
819 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
820 rettv->vval.v_number = (varnumber_T)(t - str); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
821 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
822 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
823 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
824 * "byteidx()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
825 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
826 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
827 f_byteidx(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
828 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
829 byteidx(argvars, rettv, FALSE); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
830 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
831 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
832 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
833 * "byteidxcomp()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
834 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
835 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
836 f_byteidxcomp(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
837 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
838 byteidx(argvars, rettv, TRUE); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
839 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
840 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
841 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
842 * "charidx()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
843 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
844 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
845 f_charidx(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
846 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
847 char_u *str; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
848 varnumber_T idx; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
849 varnumber_T countcc = FALSE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
850 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
851 int len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
852 int (*ptr2len)(char_u *); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
853 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
854 rettv->vval.v_number = -1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
855 |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
856 if (in_vim9script() |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
857 && (check_for_string_arg(argvars, 0) == FAIL |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
858 || check_for_number_arg(argvars, 1) == FAIL |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
859 || check_for_opt_bool_arg(argvars, 2) == FAIL)) |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
860 return; |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
861 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
862 if (argvars[0].v_type != VAR_STRING || argvars[1].v_type != VAR_NUMBER |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
863 || (argvars[2].v_type != VAR_UNKNOWN |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
864 && argvars[2].v_type != VAR_NUMBER |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
865 && argvars[2].v_type != VAR_BOOL)) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
866 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
867 emsg(_(e_invarg)); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
868 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
869 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
870 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
871 str = tv_get_string_chk(&argvars[0]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
872 idx = tv_get_number_chk(&argvars[1], NULL); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
873 if (str == NULL || idx < 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
874 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
875 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
876 if (argvars[2].v_type != VAR_UNKNOWN) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
877 countcc = tv_get_bool(&argvars[2]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
878 if (countcc < 0 || countcc > 1) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
879 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
880 semsg(_(e_using_number_as_bool_nr), countcc); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
881 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
882 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
883 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
884 if (enc_utf8 && countcc) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
885 ptr2len = utf_ptr2len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
886 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
887 ptr2len = mb_ptr2len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
888 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
889 for (p = str, len = 0; p <= str + idx; len++) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
890 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
891 if (*p == NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
892 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
893 p += ptr2len(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
894 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
895 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
896 rettv->vval.v_number = len > 0 ? len - 1 : 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
897 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
898 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
899 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
900 * "str2list()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
901 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
902 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
903 f_str2list(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
904 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
905 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
906 int utf8 = FALSE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
907 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
908 if (rettv_list_alloc(rettv) == FAIL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
909 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
910 |
25272
712e867f9721
patch 8.2.3173: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25252
diff
changeset
|
911 if (in_vim9script() |
712e867f9721
patch 8.2.3173: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25252
diff
changeset
|
912 && (check_for_string_arg(argvars, 0) == FAIL |
25302
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
913 || check_for_opt_bool_arg(argvars, 1) == FAIL)) |
25272
712e867f9721
patch 8.2.3173: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25252
diff
changeset
|
914 return; |
712e867f9721
patch 8.2.3173: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25252
diff
changeset
|
915 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
916 if (argvars[1].v_type != VAR_UNKNOWN) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
917 utf8 = (int)tv_get_bool_chk(&argvars[1], NULL); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
918 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
919 p = tv_get_string(&argvars[0]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
920 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
921 if (has_mbyte || utf8) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
922 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
923 int (*ptr2len)(char_u *); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
924 int (*ptr2char)(char_u *); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
925 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
926 if (utf8 || enc_utf8) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
927 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
928 ptr2len = utf_ptr2len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
929 ptr2char = utf_ptr2char; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
930 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
931 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
932 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
933 ptr2len = mb_ptr2len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
934 ptr2char = mb_ptr2char; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
935 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
936 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
937 for ( ; *p != NUL; p += (*ptr2len)(p)) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
938 list_append_number(rettv->vval.v_list, (*ptr2char)(p)); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
939 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
940 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
941 for ( ; *p != NUL; ++p) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
942 list_append_number(rettv->vval.v_list, *p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
943 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
944 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
945 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
946 * "str2nr()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
947 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
948 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
949 f_str2nr(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
950 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
951 int base = 10; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
952 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
953 varnumber_T n; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
954 int what = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
955 int isneg; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
956 |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
957 if (in_vim9script() |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
958 && (check_for_string_arg(argvars, 0) == FAIL |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
959 || check_for_opt_number_arg(argvars, 1) == FAIL |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
960 || (argvars[1].v_type != VAR_UNKNOWN |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
961 && check_for_opt_bool_arg(argvars, 2) == FAIL))) |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
962 return; |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
963 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
964 if (argvars[1].v_type != VAR_UNKNOWN) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
965 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
966 base = (int)tv_get_number(&argvars[1]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
967 if (base != 2 && base != 8 && base != 10 && base != 16) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
968 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
969 emsg(_(e_invarg)); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
970 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
971 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
972 if (argvars[2].v_type != VAR_UNKNOWN && tv_get_bool(&argvars[2])) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
973 what |= STR2NR_QUOTE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
974 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
975 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
976 p = skipwhite(tv_get_string_strict(&argvars[0])); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
977 isneg = (*p == '-'); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
978 if (*p == '+' || *p == '-') |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
979 p = skipwhite(p + 1); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
980 switch (base) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
981 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
982 case 2: what |= STR2NR_BIN + STR2NR_FORCE; break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
983 case 8: what |= STR2NR_OCT + STR2NR_OOCT + STR2NR_FORCE; break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
984 case 16: what |= STR2NR_HEX + STR2NR_FORCE; break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
985 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
986 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0, FALSE); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
987 // Text after the number is silently ignored. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
988 if (isneg) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
989 rettv->vval.v_number = -n; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
990 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
991 rettv->vval.v_number = n; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
992 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
993 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
994 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
995 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
996 * "strgetchar()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
997 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
998 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
999 f_strgetchar(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1000 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1001 char_u *str; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1002 int len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1003 int error = FALSE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1004 int charidx; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1005 int byteidx = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1006 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1007 rettv->vval.v_number = -1; |
25252
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1008 |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1009 if (in_vim9script() |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1010 && (check_for_string_arg(argvars, 0) == FAIL |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1011 || check_for_number_arg(argvars, 1) == FAIL)) |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1012 return; |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1013 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1014 str = tv_get_string_chk(&argvars[0]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1015 if (str == NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1016 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1017 len = (int)STRLEN(str); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1018 charidx = (int)tv_get_number_chk(&argvars[1], &error); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1019 if (error) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1020 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1021 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1022 while (charidx >= 0 && byteidx < len) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1023 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1024 if (charidx == 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1025 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1026 rettv->vval.v_number = mb_ptr2char(str + byteidx); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1027 break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1028 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1029 --charidx; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1030 byteidx += MB_CPTR2LEN(str + byteidx); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1031 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1032 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1033 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1034 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1035 * "stridx()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1036 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1037 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1038 f_stridx(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1039 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1040 char_u buf[NUMBUFLEN]; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1041 char_u *needle; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1042 char_u *haystack; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1043 char_u *save_haystack; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1044 char_u *pos; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1045 int start_idx; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1046 |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1047 if (in_vim9script() |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1048 && (check_for_string_arg(argvars, 0) == FAIL |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1049 || check_for_string_arg(argvars, 1) == FAIL |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1050 || check_for_opt_number_arg(argvars, 2) == FAIL)) |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1051 return; |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1052 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1053 needle = tv_get_string_chk(&argvars[1]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1054 save_haystack = haystack = tv_get_string_buf_chk(&argvars[0], buf); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1055 rettv->vval.v_number = -1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1056 if (needle == NULL || haystack == NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1057 return; // type error; errmsg already given |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1058 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1059 if (argvars[2].v_type != VAR_UNKNOWN) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1060 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1061 int error = FALSE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1062 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1063 start_idx = (int)tv_get_number_chk(&argvars[2], &error); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1064 if (error || start_idx >= (int)STRLEN(haystack)) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1065 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1066 if (start_idx >= 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1067 haystack += start_idx; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1068 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1069 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1070 pos = (char_u *)strstr((char *)haystack, (char *)needle); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1071 if (pos != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1072 rettv->vval.v_number = (varnumber_T)(pos - save_haystack); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1073 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1074 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1075 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1076 * "string()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1077 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1078 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1079 f_string(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1080 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1081 char_u *tofree; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1082 char_u numbuf[NUMBUFLEN]; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1083 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1084 rettv->v_type = VAR_STRING; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1085 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1086 get_copyID()); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1087 // Make a copy if we have a value but it's not in allocated memory. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1088 if (rettv->vval.v_string != NULL && tofree == NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1089 rettv->vval.v_string = vim_strsave(rettv->vval.v_string); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1090 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1091 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1092 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1093 * "strlen()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1094 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1095 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1096 f_strlen(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1097 { |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1098 if (in_vim9script() |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1099 && check_for_string_or_number_arg(argvars, 0) == FAIL) |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1100 return; |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1101 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1102 rettv->vval.v_number = (varnumber_T)(STRLEN( |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1103 tv_get_string(&argvars[0]))); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1104 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1105 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1106 static void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1107 strchar_common(typval_T *argvars, typval_T *rettv, int skipcc) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1108 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1109 char_u *s = tv_get_string(&argvars[0]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1110 varnumber_T len = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1111 int (*func_mb_ptr2char_adv)(char_u **pp); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1112 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1113 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1114 while (*s != NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1115 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1116 func_mb_ptr2char_adv(&s); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1117 ++len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1118 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1119 rettv->vval.v_number = len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1120 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1121 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1122 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1123 * "strcharlen()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1124 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1125 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1126 f_strcharlen(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1127 { |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1128 if (in_vim9script() |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1129 && check_for_string_or_number_arg(argvars, 0) == FAIL) |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1130 return; |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1131 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1132 strchar_common(argvars, rettv, TRUE); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1133 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1134 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1135 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1136 * "strchars()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1137 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1138 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1139 f_strchars(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1140 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1141 varnumber_T skipcc = FALSE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1142 |
25272
712e867f9721
patch 8.2.3173: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25252
diff
changeset
|
1143 if (in_vim9script() |
712e867f9721
patch 8.2.3173: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25252
diff
changeset
|
1144 && (check_for_string_arg(argvars, 0) == FAIL |
25302
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1145 || check_for_opt_bool_arg(argvars, 1) == FAIL)) |
25272
712e867f9721
patch 8.2.3173: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25252
diff
changeset
|
1146 return; |
712e867f9721
patch 8.2.3173: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25252
diff
changeset
|
1147 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1148 if (argvars[1].v_type != VAR_UNKNOWN) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1149 skipcc = tv_get_bool(&argvars[1]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1150 if (skipcc < 0 || skipcc > 1) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1151 semsg(_(e_using_number_as_bool_nr), skipcc); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1152 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1153 strchar_common(argvars, rettv, skipcc); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1154 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1155 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1156 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1157 * "strdisplaywidth()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1158 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1159 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1160 f_strdisplaywidth(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1161 { |
25252
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1162 char_u *s; |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1163 int col = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1164 |
25252
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1165 rettv->vval.v_number = -1; |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1166 |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1167 if (in_vim9script() |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1168 && (check_for_string_arg(argvars, 0) == FAIL |
25302
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1169 || check_for_opt_number_arg(argvars, 1) == FAIL)) |
25252
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1170 return; |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1171 |
acda780ffc3e
patch 8.2.3162: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25206
diff
changeset
|
1172 s = tv_get_string(&argvars[0]); |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1173 if (argvars[1].v_type != VAR_UNKNOWN) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1174 col = (int)tv_get_number(&argvars[1]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1175 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1176 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1177 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1178 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1179 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1180 * "strwidth()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1181 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1182 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1183 f_strwidth(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1184 { |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1185 char_u *s; |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1186 |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1187 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1188 return; |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1189 |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1190 s = tv_get_string_strict(&argvars[0]); |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1191 rettv->vval.v_number = (varnumber_T)(mb_string2cells(s, -1)); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1192 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1193 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1194 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1195 * "strcharpart()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1196 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1197 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1198 f_strcharpart(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1199 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1200 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1201 int nchar; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1202 int nbyte = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1203 int charlen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1204 int skipcc = FALSE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1205 int len = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1206 int slen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1207 int error = FALSE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1208 |
25302
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1209 if (in_vim9script() |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1210 && (check_for_string_arg(argvars, 0) == FAIL |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1211 || check_for_number_arg(argvars, 1) == FAIL |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1212 || check_for_opt_number_arg(argvars, 2) == FAIL |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1213 || (argvars[2].v_type != VAR_UNKNOWN |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1214 && check_for_opt_bool_arg(argvars, 3) == FAIL))) |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1215 return; |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1216 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1217 p = tv_get_string(&argvars[0]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1218 slen = (int)STRLEN(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1219 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1220 nchar = (int)tv_get_number_chk(&argvars[1], &error); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1221 if (!error) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1222 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1223 if (argvars[2].v_type != VAR_UNKNOWN |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1224 && argvars[3].v_type != VAR_UNKNOWN) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1225 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1226 skipcc = tv_get_bool(&argvars[3]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1227 if (skipcc < 0 || skipcc > 1) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1228 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1229 semsg(_(e_using_number_as_bool_nr), skipcc); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1230 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1231 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1232 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1233 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1234 if (nchar > 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1235 while (nchar > 0 && nbyte < slen) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1236 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1237 if (skipcc) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1238 nbyte += mb_ptr2len(p + nbyte); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1239 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1240 nbyte += MB_CPTR2LEN(p + nbyte); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1241 --nchar; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1242 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1243 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1244 nbyte = nchar; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1245 if (argvars[2].v_type != VAR_UNKNOWN) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1246 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1247 charlen = (int)tv_get_number(&argvars[2]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1248 while (charlen > 0 && nbyte + len < slen) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1249 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1250 int off = nbyte + len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1251 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1252 if (off < 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1253 len += 1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1254 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1255 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1256 if (skipcc) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1257 len += mb_ptr2len(p + off); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1258 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1259 len += MB_CPTR2LEN(p + off); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1260 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1261 --charlen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1262 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1263 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1264 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1265 len = slen - nbyte; // default: all bytes that are available. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1266 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1267 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1268 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1269 * Only return the overlap between the specified part and the actual |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1270 * string. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1271 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1272 if (nbyte < 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1273 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1274 len += nbyte; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1275 nbyte = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1276 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1277 else if (nbyte > slen) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1278 nbyte = slen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1279 if (len < 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1280 len = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1281 else if (nbyte + len > slen) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1282 len = slen - nbyte; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1283 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1284 rettv->v_type = VAR_STRING; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1285 rettv->vval.v_string = vim_strnsave(p + nbyte, len); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1286 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1287 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1288 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1289 * "strpart()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1290 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1291 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1292 f_strpart(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1293 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1294 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1295 int n; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1296 int len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1297 int slen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1298 int error = FALSE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1299 |
25302
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1300 if (in_vim9script() |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1301 && (check_for_string_arg(argvars, 0) == FAIL |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1302 || check_for_number_arg(argvars, 1) == FAIL |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1303 || check_for_opt_number_arg(argvars, 2) == FAIL |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1304 || (argvars[2].v_type != VAR_UNKNOWN |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1305 && check_for_opt_bool_arg(argvars, 3) == FAIL))) |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1306 return; |
4d3c68196d05
patch 8.2.3188: Vim9: argument types are not checked at compile time
Bram Moolenaar <Bram@vim.org>
parents:
25272
diff
changeset
|
1307 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1308 p = tv_get_string(&argvars[0]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1309 slen = (int)STRLEN(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1310 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1311 n = (int)tv_get_number_chk(&argvars[1], &error); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1312 if (error) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1313 len = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1314 else if (argvars[2].v_type != VAR_UNKNOWN) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1315 len = (int)tv_get_number(&argvars[2]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1316 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1317 len = slen - n; // default len: all bytes that are available. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1318 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1319 // Only return the overlap between the specified part and the actual |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1320 // string. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1321 if (n < 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1322 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1323 len += n; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1324 n = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1325 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1326 else if (n > slen) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1327 n = slen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1328 if (len < 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1329 len = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1330 else if (n + len > slen) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1331 len = slen - n; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1332 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1333 if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1334 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1335 int off; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1336 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1337 // length in characters |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1338 for (off = n; off < slen && len > 0; --len) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1339 off += mb_ptr2len(p + off); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1340 len = off - n; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1341 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1342 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1343 rettv->v_type = VAR_STRING; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1344 rettv->vval.v_string = vim_strnsave(p + n, len); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1345 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1346 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1347 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1348 * "strridx()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1349 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1350 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1351 f_strridx(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1352 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1353 char_u buf[NUMBUFLEN]; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1354 char_u *needle; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1355 char_u *haystack; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1356 char_u *rest; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1357 char_u *lastmatch = NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1358 int haystack_len, end_idx; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1359 |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1360 if (in_vim9script() |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1361 && (check_for_string_arg(argvars, 0) == FAIL |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1362 || check_for_string_arg(argvars, 1) == FAIL |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1363 || check_for_opt_number_arg(argvars, 2) == FAIL)) |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1364 return; |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1365 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1366 needle = tv_get_string_chk(&argvars[1]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1367 haystack = tv_get_string_buf_chk(&argvars[0], buf); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1368 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1369 rettv->vval.v_number = -1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1370 if (needle == NULL || haystack == NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1371 return; // type error; errmsg already given |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1372 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1373 haystack_len = (int)STRLEN(haystack); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1374 if (argvars[2].v_type != VAR_UNKNOWN) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1375 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1376 // Third argument: upper limit for index |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1377 end_idx = (int)tv_get_number_chk(&argvars[2], NULL); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1378 if (end_idx < 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1379 return; // can never find a match |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1380 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1381 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1382 end_idx = haystack_len; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1383 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1384 if (*needle == NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1385 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1386 // Empty string matches past the end. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1387 lastmatch = haystack + end_idx; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1388 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1389 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1390 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1391 for (rest = haystack; *rest != '\0'; ++rest) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1392 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1393 rest = (char_u *)strstr((char *)rest, (char *)needle); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1394 if (rest == NULL || rest > haystack + end_idx) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1395 break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1396 lastmatch = rest; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1397 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1398 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1399 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1400 if (lastmatch == NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1401 rettv->vval.v_number = -1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1402 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1403 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1404 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1405 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1406 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1407 * "strtrans()" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1408 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1409 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1410 f_strtrans(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1411 { |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1412 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1413 return; |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1414 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1415 rettv->v_type = VAR_STRING; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1416 rettv->vval.v_string = transstr(tv_get_string(&argvars[0])); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1417 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1418 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1419 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1420 * "tolower(string)" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1421 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1422 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1423 f_tolower(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1424 { |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1425 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1426 return; |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1427 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1428 rettv->v_type = VAR_STRING; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1429 rettv->vval.v_string = strlow_save(tv_get_string(&argvars[0])); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1430 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1431 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1432 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1433 * "toupper(string)" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1434 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1435 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1436 f_toupper(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1437 { |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1438 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1439 return; |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1440 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1441 rettv->v_type = VAR_STRING; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1442 rettv->vval.v_string = strup_save(tv_get_string(&argvars[0])); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1443 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1444 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1445 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1446 * "tr(string, fromstr, tostr)" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1447 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1448 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1449 f_tr(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1450 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1451 char_u *in_str; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1452 char_u *fromstr; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1453 char_u *tostr; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1454 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1455 int inlen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1456 int fromlen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1457 int tolen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1458 int idx; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1459 char_u *cpstr; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1460 int cplen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1461 int first = TRUE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1462 char_u buf[NUMBUFLEN]; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1463 char_u buf2[NUMBUFLEN]; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1464 garray_T ga; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1465 |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1466 if (in_vim9script() |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1467 && (check_for_string_arg(argvars, 0) == FAIL |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1468 || check_for_string_arg(argvars, 1) == FAIL |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1469 || check_for_string_arg(argvars, 2) == FAIL)) |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1470 return; |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1471 |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1472 in_str = tv_get_string(&argvars[0]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1473 fromstr = tv_get_string_buf_chk(&argvars[1], buf); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1474 tostr = tv_get_string_buf_chk(&argvars[2], buf2); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1475 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1476 // Default return value: empty string. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1477 rettv->v_type = VAR_STRING; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1478 rettv->vval.v_string = NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1479 if (fromstr == NULL || tostr == NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1480 return; // type error; errmsg already given |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1481 ga_init2(&ga, (int)sizeof(char), 80); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1482 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1483 if (!has_mbyte) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1484 // not multi-byte: fromstr and tostr must be the same length |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1485 if (STRLEN(fromstr) != STRLEN(tostr)) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1486 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1487 error: |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1488 semsg(_(e_invarg2), fromstr); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1489 ga_clear(&ga); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1490 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1491 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1492 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1493 // fromstr and tostr have to contain the same number of chars |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1494 while (*in_str != NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1495 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1496 if (has_mbyte) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1497 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1498 inlen = (*mb_ptr2len)(in_str); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1499 cpstr = in_str; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1500 cplen = inlen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1501 idx = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1502 for (p = fromstr; *p != NUL; p += fromlen) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1503 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1504 fromlen = (*mb_ptr2len)(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1505 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1506 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1507 for (p = tostr; *p != NUL; p += tolen) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1508 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1509 tolen = (*mb_ptr2len)(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1510 if (idx-- == 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1511 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1512 cplen = tolen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1513 cpstr = p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1514 break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1515 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1516 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1517 if (*p == NUL) // tostr is shorter than fromstr |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1518 goto error; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1519 break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1520 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1521 ++idx; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1522 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1523 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1524 if (first && cpstr == in_str) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1525 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1526 // Check that fromstr and tostr have the same number of |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1527 // (multi-byte) characters. Done only once when a character |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1528 // of in_str doesn't appear in fromstr. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1529 first = FALSE; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1530 for (p = tostr; *p != NUL; p += tolen) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1531 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1532 tolen = (*mb_ptr2len)(p); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1533 --idx; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1534 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1535 if (idx != 0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1536 goto error; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1537 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1538 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1539 (void)ga_grow(&ga, cplen); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1540 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1541 ga.ga_len += cplen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1542 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1543 in_str += inlen; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1544 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1545 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1546 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1547 // When not using multi-byte chars we can do it faster. |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1548 p = vim_strchr(fromstr, *in_str); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1549 if (p != NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1550 ga_append(&ga, tostr[p - fromstr]); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1551 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1552 ga_append(&ga, *in_str); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1553 ++in_str; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1554 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1555 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1556 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1557 // add a terminating NUL |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1558 (void)ga_grow(&ga, 1); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1559 ga_append(&ga, NUL); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1560 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1561 rettv->vval.v_string = ga.ga_data; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1562 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1563 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1564 /* |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1565 * "trim({expr})" function |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1566 */ |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1567 void |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1568 f_trim(typval_T *argvars, typval_T *rettv) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1569 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1570 char_u buf1[NUMBUFLEN]; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1571 char_u buf2[NUMBUFLEN]; |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1572 char_u *head; |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1573 char_u *mask = NULL; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1574 char_u *tail; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1575 char_u *prev; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1576 char_u *p; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1577 int c1; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1578 int dir = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1579 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1580 rettv->v_type = VAR_STRING; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1581 rettv->vval.v_string = NULL; |
25384
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1582 |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1583 if (in_vim9script() |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1584 && (check_for_string_arg(argvars, 0) == FAIL |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1585 || check_for_opt_string_arg(argvars, 1) == FAIL |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1586 || (argvars[1].v_type != VAR_UNKNOWN |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1587 && check_for_opt_number_arg(argvars, 2) == FAIL))) |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1588 return; |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1589 |
e8e2c4d33b9b
patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents:
25302
diff
changeset
|
1590 head = tv_get_string_buf_chk(&argvars[0], buf1); |
25206
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1591 if (head == NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1592 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1593 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1594 if (argvars[1].v_type != VAR_UNKNOWN && argvars[1].v_type != VAR_STRING) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1595 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1596 semsg(_(e_invarg2), tv_get_string(&argvars[1])); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1597 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1598 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1599 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1600 if (argvars[1].v_type == VAR_STRING) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1601 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1602 mask = tv_get_string_buf_chk(&argvars[1], buf2); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1603 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1604 if (argvars[2].v_type != VAR_UNKNOWN) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1605 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1606 int error = 0; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1607 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1608 // leading or trailing characters to trim |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1609 dir = (int)tv_get_number_chk(&argvars[2], &error); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1610 if (error) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1611 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1612 if (dir < 0 || dir > 2) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1613 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1614 semsg(_(e_invarg2), tv_get_string(&argvars[2])); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1615 return; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1616 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1617 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1618 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1619 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1620 if (dir == 0 || dir == 1) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1621 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1622 // Trim leading characters |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1623 while (*head != NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1624 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1625 c1 = PTR2CHAR(head); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1626 if (mask == NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1627 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1628 if (c1 > ' ' && c1 != 0xa0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1629 break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1630 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1631 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1632 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1633 for (p = mask; *p != NUL; MB_PTR_ADV(p)) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1634 if (c1 == PTR2CHAR(p)) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1635 break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1636 if (*p == NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1637 break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1638 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1639 MB_PTR_ADV(head); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1640 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1641 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1642 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1643 tail = head + STRLEN(head); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1644 if (dir == 0 || dir == 2) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1645 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1646 // Trim trailing characters |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1647 for (; tail > head; tail = prev) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1648 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1649 prev = tail; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1650 MB_PTR_BACK(head, prev); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1651 c1 = PTR2CHAR(prev); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1652 if (mask == NULL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1653 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1654 if (c1 > ' ' && c1 != 0xa0) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1655 break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1656 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1657 else |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1658 { |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1659 for (p = mask; *p != NUL; MB_PTR_ADV(p)) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1660 if (c1 == PTR2CHAR(p)) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1661 break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1662 if (*p == NUL) |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1663 break; |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1664 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1665 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1666 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1667 rettv->vval.v_string = vim_strnsave(head, tail - head); |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1668 } |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1669 |
dc66d0284518
patch 8.2.3139: functions for string manipulation are spread out
Bram Moolenaar <Bram@vim.org>
parents:
diff
changeset
|
1670 #endif |