annotate src/strings.c @ 25252:acda780ffc3e v8.2.3162

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