1 /**
2  * Copyright (c) 2019, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef lnav_string_util_hh
31 #define lnav_string_util_hh
32 
33 #include <string.h>
34 #include <string>
35 #include <vector>
36 
37 #include "ww898/cp_utf8.hpp"
38 
39 void scrub_to_utf8(char *buffer, size_t length);
40 
is_line_ending(char ch)41 inline bool is_line_ending(char ch) {
42     return ch == '\r' || ch == '\n';
43 }
44 
45 size_t unquote(char *dst, const char *str, size_t len);
46 
47 size_t unquote_w3c(char *dst, const char *str, size_t len);
48 
startswith(const char * str,const char * prefix)49 inline bool startswith(const char *str, const char *prefix)
50 {
51     return strncmp(str, prefix, strlen(prefix)) == 0;
52 }
53 
startswith(const std::string & str,const char * prefix)54 inline bool startswith(const std::string &str, const char *prefix)
55 {
56     return startswith(str.c_str(), prefix);
57 }
58 
startswith(const std::string & str,const std::string & prefix)59 inline bool startswith(const std::string &str, const std::string& prefix)
60 {
61     return startswith(str.c_str(), prefix.c_str());
62 }
63 
endswith(const char * str,const char * suffix)64 inline bool endswith(const char *str, const char *suffix)
65 {
66     size_t len = strlen(str), suffix_len = strlen(suffix);
67 
68     if (suffix_len > len) {
69         return false;
70     }
71 
72     return strcmp(&str[len - suffix_len], suffix) == 0;
73 }
74 
75 template<int N>
endswith(const std::string & str,const char (& suffix)[N])76 inline bool endswith(const std::string& str, const char (&suffix) [N])
77 {
78     if (N - 1 > str.length()) {
79         return false;
80     }
81 
82     return strcmp(&str[str.size() - (N - 1)], suffix) == 0;
83 }
84 
85 void truncate_to(std::string &str, size_t max_char_len);
86 
trim(const std::string & str)87 inline std::string trim(const std::string &str)
88 {
89     std::string::size_type start, end;
90 
91     for (start = 0; start < str.size() && isspace(str[start]); start++);
92     for (end = str.size(); end > 0 && isspace(str[end - 1]); end--);
93 
94     return str.substr(start, end - start);
95 }
96 
tolower(const char * str)97 inline std::string tolower(const char *str)
98 {
99     std::string retval;
100 
101     for (int lpc = 0; str[lpc]; lpc++) {
102         retval.push_back(::tolower(str[lpc]));
103     }
104 
105     return retval;
106 }
107 
tolower(const std::string & str)108 inline std::string tolower(const std::string &str)
109 {
110     return tolower(str.c_str());
111 }
112 
toupper(const char * str)113 inline std::string toupper(const char *str)
114 {
115     std::string retval;
116 
117     for (int lpc = 0; str[lpc]; lpc++) {
118         retval.push_back(::toupper(str[lpc]));
119     }
120 
121     return retval;
122 }
123 
toupper(const std::string & str)124 inline std::string toupper(const std::string &str)
125 {
126     return toupper(str.c_str());
127 }
128 
utf8_char_to_byte_index(const std::string & str,ssize_t ch_index)129 inline ssize_t utf8_char_to_byte_index(const std::string &str, ssize_t ch_index)
130 {
131     ssize_t retval = 0;
132 
133     while (ch_index > 0) {
134         auto ch_len = ww898::utf::utf8::char_size([&str, retval]() {
135             return std::make_pair(str[retval], str.length() - retval - 1);
136         }).unwrapOr(1);
137 
138         retval += ch_len;
139         ch_index -= 1;
140     }
141 
142     return retval;
143 }
144 
utf8_string_length(const char * str,ssize_t len=-1)145 inline Result<size_t, const char *> utf8_string_length(const char *str, ssize_t len = -1)
146 {
147     size_t retval = 0;
148 
149     if (len == -1) {
150         len = strlen(str);
151     }
152 
153     for (ssize_t byte_index = 0; byte_index < len;) {
154         auto ch_size = TRY(ww898::utf::utf8::char_size([str, len, byte_index]() {
155             return std::make_pair(str[byte_index], len - byte_index);
156         }));
157         byte_index += ch_size;
158         retval += 1;
159     }
160 
161     return Ok(retval);
162 }
163 
utf8_string_length(const std::string & str)164 inline Result<size_t, const char *> utf8_string_length(const std::string& str)
165 {
166     return utf8_string_length(str.c_str(), str.length());
167 }
168 
169 bool is_url(const char *fn);
170 
171 size_t abbreviate_str(char *str, size_t len, size_t max_len);
172 
173 void split_ws(const std::string &str, std::vector<std::string> &toks_out);
174 
175 std::string repeat(const std::string& input, size_t num);
176 
177 std::string center_str(const std::string& subject, size_t width);
178 
179 template<typename T>
180 size_t strtonum(T &num_out, const char *data, size_t len);
181 
182 #endif
183