1 //===-- String utils --------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LIBC_SRC_STRING_STRING_UTILS_H
10 #define LIBC_SRC_STRING_STRING_UTILS_H
11 
12 #include "src/__support/CPP/Bitset.h"
13 #include "src/__support/common.h"
14 #include "src/string/memory_utils/memcpy_implementations.h"
15 #include "src/string/memory_utils/memset_implementations.h"
16 #include <stddef.h> // size_t
17 
18 namespace __llvm_libc {
19 namespace internal {
20 
21 // Returns the length of a string, denoted by the first occurrence
22 // of a null terminator.
string_length(const char * src)23 static inline size_t string_length(const char *src) {
24   size_t length;
25   for (length = 0; *src; ++src, ++length)
26     ;
27   return length;
28 }
29 
30 // Returns the first occurrence of 'ch' within the first 'n' characters of
31 // 'src'. If 'ch' is not found, returns nullptr.
find_first_character(const unsigned char * src,unsigned char ch,size_t n)32 static inline void *find_first_character(const unsigned char *src,
33                                          unsigned char ch, size_t n) {
34   for (; n && *src != ch; --n, ++src)
35     ;
36   return n ? const_cast<unsigned char *>(src) : nullptr;
37 }
38 
39 // Returns the maximum length span that contains only characters not found in
40 // 'segment'. If no characters are found, returns the length of 'src'.
complementary_span(const char * src,const char * segment)41 static inline size_t complementary_span(const char *src, const char *segment) {
42   const char *initial = src;
43   cpp::Bitset<256> bitset;
44 
45   for (; *segment; ++segment)
46     bitset.set(*segment);
47   for (; *src && !bitset.test(*src); ++src)
48     ;
49   return src - initial;
50 }
51 
52 // Given the similarities between strtok and strtok_r, we can implement both
53 // using a utility function. On the first call, 'src' is scanned for the
54 // first character not found in 'delimiter_string'. Once found, it scans until
55 // the first character in the 'delimiter_string' or the null terminator is
56 // found. We define this span as a token. The end of the token is appended with
57 // a null terminator, and the token is returned. The point where the last token
58 // is found is then stored within 'context' for subsequent calls. Subsequent
59 // calls will use 'context' when a nullptr is passed in for 'src'. Once the null
60 // terminating character is reached, returns a nullptr.
string_token(char * __restrict src,const char * __restrict delimiter_string,char ** __restrict saveptr)61 static inline char *string_token(char *__restrict src,
62                                  const char *__restrict delimiter_string,
63                                  char **__restrict saveptr) {
64   // Return nullptr immediately if both src AND saveptr are nullptr
65   if (unlikely(src == nullptr && ((src = *saveptr) == nullptr)))
66     return nullptr;
67 
68   cpp::Bitset<256> delimiter_set;
69   for (; *delimiter_string != '\0'; ++delimiter_string)
70     delimiter_set.set(*delimiter_string);
71 
72   for (; *src != '\0' && delimiter_set.test(*src); ++src)
73     ;
74   if (*src == '\0') {
75     *saveptr = src;
76     return nullptr;
77   }
78   char *token = src;
79   for (; *src != '\0'; ++src) {
80     if (delimiter_set.test(*src)) {
81       *src = '\0';
82       ++src;
83       break;
84     }
85   }
86   *saveptr = src;
87   return token;
88 }
89 
strlcpy(char * __restrict dst,const char * __restrict src,size_t size)90 static inline size_t strlcpy(char *__restrict dst, const char *__restrict src,
91                              size_t size) {
92   size_t len = internal::string_length(src);
93   if (!size)
94     return len;
95   size_t n = len < size - 1 ? len : size - 1;
96   inline_memcpy(dst, src, n);
97   inline_memset(dst + n, 0, size - n);
98   return len;
99 }
100 
101 } // namespace internal
102 } // namespace __llvm_libc
103 
104 #endif //  LIBC_SRC_STRING_STRING_UTILS_H
105