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