aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/strings.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/strings.c')
-rw-r--r--src/nvim/strings.c99
1 files changed, 60 insertions, 39 deletions
diff --git a/src/nvim/strings.c b/src/nvim/strings.c
index d34014ac75..f22b5ebf7e 100644
--- a/src/nvim/strings.c
+++ b/src/nvim/strings.c
@@ -50,7 +50,8 @@
/*
* Copy "string" into newly allocated memory.
*/
-char_u *vim_strsave(char_u *string) FUNC_ATTR_NONNULL_RET
+char_u *vim_strsave(const char_u *string)
+ FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
return (char_u *)xstrdup((char *)string);
}
@@ -61,7 +62,8 @@ char_u *vim_strsave(char_u *string) FUNC_ATTR_NONNULL_RET
* The allocated memory always has size "len + 1", also when "string" is
* shorter.
*/
-char_u *vim_strnsave(char_u *string, int len) FUNC_ATTR_NONNULL_RET
+char_u *vim_strnsave(const char_u *string, int len)
+ FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
return (char_u *)strncpy(xmallocz(len), (char *)string, len);
}
@@ -70,8 +72,8 @@ char_u *vim_strnsave(char_u *string, int len) FUNC_ATTR_NONNULL_RET
* Same as vim_strsave(), but any characters found in esc_chars are preceded
* by a backslash.
*/
-char_u *vim_strsave_escaped(char_u *string, char_u *esc_chars)
- FUNC_ATTR_NONNULL_RET
+char_u *vim_strsave_escaped(const char_u *string, const char_u *esc_chars)
+ FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
return vim_strsave_escaped_ext(string, esc_chars, '\\', FALSE);
}
@@ -81,8 +83,9 @@ char_u *vim_strsave_escaped(char_u *string, char_u *esc_chars)
* characters where rem_backslash() would remove the backslash.
* Escape the characters with "cc".
*/
-char_u *vim_strsave_escaped_ext(char_u *string, char_u *esc_chars, int cc, int bsl)
- FUNC_ATTR_NONNULL_RET
+char_u *vim_strsave_escaped_ext(const char_u *string, const char_u *esc_chars,
+ int cc, int bsl)
+ FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
unsigned length;
int l;
@@ -92,7 +95,7 @@ char_u *vim_strsave_escaped_ext(char_u *string, char_u *esc_chars, int cc, int b
* Then allocate the memory and insert them.
*/
length = 1; /* count the trailing NUL */
- for (char_u *p = string; *p; p++) {
+ for (const char_u *p = string; *p; p++) {
if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) {
length += l; /* count a multibyte char */
p += l - 1;
@@ -105,7 +108,7 @@ char_u *vim_strsave_escaped_ext(char_u *string, char_u *esc_chars, int cc, int b
char_u *escaped_string = xmalloc(length);
char_u *p2 = escaped_string;
- for (char_u *p = string; *p; p++) {
+ for (const char_u *p = string; *p; p++) {
if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) {
memmove(p2, p, (size_t)l);
p2 += l;
@@ -131,10 +134,10 @@ char_u *vim_strsave_escaped_ext(char_u *string, char_u *esc_chars, int cc, int b
* When "do_newline" is false do not escape newline unless it is csh shell.
* Returns the result in allocated memory.
*/
-char_u *vim_strsave_shellescape(char_u *string, bool do_special, bool do_newline)
+char_u *vim_strsave_shellescape(const char_u *string,
+ bool do_special, bool do_newline)
+ FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
- unsigned length;
- char_u *p;
char_u *d;
char_u *escaped_string;
int l;
@@ -147,8 +150,8 @@ char_u *vim_strsave_shellescape(char_u *string, bool do_special, bool do_newline
csh_like = csh_like_shell();
/* First count the number of extra bytes required. */
- length = (unsigned)STRLEN(string) + 3; /* two quotes and a trailing NUL */
- for (p = string; *p != NUL; mb_ptr_adv(p)) {
+ size_t length = STRLEN(string) + 3; // two quotes and a trailing NUL
+ for (const char_u *p = string; *p != NUL; mb_ptr_adv(p)) {
if (*p == '\'')
length += 3; /* ' => '\'' */
if ((*p == '\n' && (csh_like || do_newline))
@@ -170,7 +173,7 @@ char_u *vim_strsave_shellescape(char_u *string, bool do_special, bool do_newline
/* add opening quote */
*d++ = '\'';
- for (p = string; *p != NUL; ) {
+ for (const char_u *p = string; *p != NUL; ) {
if (*p == '\'') {
*d++ = '\'';
*d++ = '\\';
@@ -208,7 +211,8 @@ char_u *vim_strsave_shellescape(char_u *string, bool do_special, bool do_newline
* Like vim_strsave(), but make all characters uppercase.
* This uses ASCII lower-to-upper case translation, language independent.
*/
-char_u *vim_strsave_up(char_u *string)
+char_u *vim_strsave_up(const char_u *string)
+ FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
char_u *p1;
@@ -221,7 +225,8 @@ char_u *vim_strsave_up(char_u *string)
* Like vim_strnsave(), but make all characters uppercase.
* This uses ASCII lower-to-upper case translation, language independent.
*/
-char_u *vim_strnsave_up(char_u *string, int len) FUNC_ATTR_NONNULL_RET
+char_u *vim_strnsave_up(const char_u *string, int len)
+ FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
char_u *p1 = vim_strnsave(string, len);
vim_strup(p1);
@@ -232,6 +237,7 @@ char_u *vim_strnsave_up(char_u *string, int len) FUNC_ATTR_NONNULL_RET
* ASCII lower-to-upper case translation, language independent.
*/
void vim_strup(char_u *p)
+ FUNC_ATTR_NONNULL_ALL
{
char_u *p2;
int c;
@@ -247,7 +253,8 @@ void vim_strup(char_u *p)
* Make string "s" all upper-case and return it in allocated memory.
* Handles multi-byte characters as well as possible.
*/
-char_u *strup_save(char_u *orig)
+char_u *strup_save(const char_u *orig)
+ FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
char_u *res = vim_strsave(orig);
@@ -290,6 +297,7 @@ char_u *strup_save(char_u *orig)
* copy a space a number of times
*/
void copy_spaces(char_u *ptr, size_t count)
+ FUNC_ATTR_NONNULL_ALL
{
size_t i = count;
char_u *p = ptr;
@@ -303,6 +311,7 @@ void copy_spaces(char_u *ptr, size_t count)
* Does not work for multi-byte characters!
*/
void copy_chars(char_u *ptr, size_t count, int c)
+ FUNC_ATTR_NONNULL_ALL
{
size_t i = count;
char_u *p = ptr;
@@ -315,6 +324,7 @@ void copy_chars(char_u *ptr, size_t count, int c)
* delete spaces at the end of a string
*/
void del_trailing_spaces(char_u *ptr)
+ FUNC_ATTR_NONNULL_ALL
{
char_u *q;
@@ -327,7 +337,8 @@ void del_trailing_spaces(char_u *ptr)
* Like strncpy(), but always terminate the result with one NUL.
* "to" must be "len + 1" long!
*/
-void vim_strncpy(char_u *to, char_u *from, size_t len)
+void vim_strncpy(char_u *restrict to, const char_u *restrict from, size_t len)
+ FUNC_ATTR_NONNULL_ALL
{
STRNCPY(to, from, len);
to[len] = NUL;
@@ -337,13 +348,15 @@ void vim_strncpy(char_u *to, char_u *from, size_t len)
* Like strcat(), but make sure the result fits in "tosize" bytes and is
* always NUL terminated.
*/
-void vim_strcat(char_u *to, char_u *from, size_t tosize)
+void vim_strcat(char_u *restrict to, const char_u *restrict from,
+ size_t tosize)
+ FUNC_ATTR_NONNULL_ALL
{
size_t tolen = STRLEN(to);
size_t fromlen = STRLEN(from);
if (tolen + fromlen + 1 > tosize) {
- memmove(to + tolen, from, tosize - tolen - 1);
+ memcpy(to + tolen, from, tosize - tolen - 1);
to[tosize - 1] = NUL;
} else
STRCPY(to + tolen, from);
@@ -355,7 +368,8 @@ void vim_strcat(char_u *to, char_u *from, size_t tosize)
* Doesn't work for multi-byte characters.
* return 0 for match, < 0 for smaller, > 0 for bigger
*/
-int vim_stricmp(char *s1, char *s2)
+int vim_stricmp(const char *s1, const char *s2)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
{
int i;
@@ -378,7 +392,8 @@ int vim_stricmp(char *s1, char *s2)
* Doesn't work for multi-byte characters.
* return 0 for match, < 0 for smaller, > 0 for bigger
*/
-int vim_strnicmp(char *s1, char *s2, size_t len)
+int vim_strnicmp(const char *s1, const char *s2, size_t len)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
{
int i;
@@ -401,16 +416,16 @@ int vim_strnicmp(char *s1, char *s2, size_t len)
* with characters from 128 to 255 correctly. It also doesn't return a
* pointer to the NUL at the end of the string.
*/
-char_u *vim_strchr(char_u *string, int c)
+char_u *vim_strchr(const char_u *string, int c)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
{
- char_u *p;
int b;
- p = string;
+ const char_u *p = string;
if (enc_utf8 && c >= 0x80) {
while (*p != NUL) {
if (utf_ptr2char(p) == c)
- return p;
+ return (char_u *) p;
p += (*mb_ptr2len)(p);
}
return NULL;
@@ -421,7 +436,7 @@ char_u *vim_strchr(char_u *string, int c)
c = ((unsigned)c >> 8) & 0xff;
while ((b = *p) != NUL) {
if (b == c && p[1] == n2)
- return p;
+ return (char_u *) p;
p += (*mb_ptr2len)(p);
}
return NULL;
@@ -429,14 +444,14 @@ char_u *vim_strchr(char_u *string, int c)
if (has_mbyte) {
while ((b = *p) != NUL) {
if (b == c)
- return p;
+ return (char_u *) p;
p += (*mb_ptr2len)(p);
}
return NULL;
}
while ((b = *p) != NUL) {
if (b == c)
- return p;
+ return (char_u *) p;
++p;
}
return NULL;
@@ -447,13 +462,14 @@ char_u *vim_strchr(char_u *string, int c)
* strings with characters above 128 correctly. It also doesn't return a
* pointer to the NUL at the end of the string.
*/
-char_u *vim_strbyte(char_u *string, int c)
+char_u *vim_strbyte(const char_u *string, int c)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
{
- char_u *p = string;
+ const char_u *p = string;
while (*p != NUL) {
if (*p == c)
- return p;
+ return (char_u *) p;
++p;
}
return NULL;
@@ -464,17 +480,18 @@ char_u *vim_strbyte(char_u *string, int c)
* Return NULL if not found.
* Does not handle multi-byte char for "c"!
*/
-char_u *vim_strrchr(char_u *string, int c)
+char_u *vim_strrchr(const char_u *string, int c)
+ FUNC_ATTR_NONNULL_RET FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
{
- char_u *retval = NULL;
- char_u *p = string;
+ const char_u *retval = NULL;
+ const char_u *p = string;
while (*p) {
if (*p == c)
retval = p;
mb_ptr_adv(p);
}
- return retval;
+ return (char_u *) retval;
}
/*
@@ -482,6 +499,7 @@ char_u *vim_strrchr(char_u *string, int c)
* can't handle characters above 128.
*/
int vim_isspace(int x)
+ FUNC_ATTR_CONST
{
return (x >= 9 && x <= 13) || x == ' ';
}
@@ -494,6 +512,7 @@ int vim_isspace(int x)
# include "strings.c.generated.h"
#endif
static int sort_compare(const void *s1, const void *s2)
+ FUNC_ATTR_NONNULL_ALL
{
return STRCMP(*(char **)s1, *(char **)s2);
}
@@ -507,9 +526,10 @@ void sort_strings(char_u **files, int count)
* Return TRUE if string "s" contains a non-ASCII character (128 or higher).
* When "s" is NULL FALSE is returned.
*/
-int has_non_ascii(char_u *s)
+int has_non_ascii(const char_u *s)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_PURE
{
- char_u *p;
+ const char_u *p;
if (s != NULL)
for (p = s; *p != NUL; ++p)
@@ -521,7 +541,8 @@ int has_non_ascii(char_u *s)
/*
* Concatenate two strings and return the result in allocated memory.
*/
-char_u *concat_str(char_u *str1, char_u *str2) FUNC_ATTR_NONNULL_RET
+char_u *concat_str(const char_u *restrict str1, const char_u *restrict str2)
+ FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
size_t l = STRLEN(str1);
char_u *dest = xmalloc(l + STRLEN(str2) + 1);