1 /*
2 * Copyright 2018 Rockchip Electronics Co. LTD
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * author: martin.cheng@rock-chips.com
17 * date: 20181126
18 */
19
20 #ifndef SRC_RT_BASE_INCLUDE_RT_STRING_UTILS_H_
21 #define SRC_RT_BASE_INCLUDE_RT_STRING_UTILS_H_
22
23 #include "rt_header.h" // NOLINT
24
25 #include <stdio.h> // NOLINT
26 #include <stdarg.h> // NOLINT
27 #include <string> // NOLINT
28 #include <string.h> // NOLINT strerror/strstr/strcmp
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 INT32 util_vsnprintf(char* buffer, size_t max_len, const char* format, va_list args);
35 INT32 util_snprintf(char* buffer, size_t max_len, const char* format, ...);
36 std::string util_vsprintf(const char* format, va_list args);
37 std::string util_sprintf(const char* format, ...);
38 std::string util_to_string(int num);
39 const char* util_to_char(const std::string buf);
40 char* util_stristr(const char* str1, const char* str2);
41 char* util_strsepstr(const char* str1, const char* str2, const char* delim);
42
43 INT32 util_strcasecmp(const char* str1, const char* str2);
44 INT32 util_strncasecmp(const char* str1, const char* str2, size_t n);
45 INT32 util_strtolower(char* dst, const char* src, INT32 length);
46 INT32 util_strtoupper(char* dst, const char* src, INT32 length);
47
48 INT32 util_strlcpy(char *dst, const char *src, INT32 size);
49 INT32 util_strlcat(char *dst, const char *src, INT32 size);
50
51 INT32 util_dump_str(INT32 fd, const char* args);
52
53 /**
54 * Locale-independent conversion of ASCII characters to uppercase.
55 */
rt_toupper(int c)56 static inline const int rt_toupper(int c) {
57 if (c >= 'a' && c <= 'z')
58 c ^= 0x20;
59 return c;
60 }
61
62 /**
63 * Locale-independent conversion of ASCII characters to lowercase.
64 */
rt_tolower(int c)65 static inline const int rt_tolower(int c) {
66 if (c >= 'A' && c <= 'Z')
67 c ^= 0x20;
68 return c;
69 }
70
71 #define RT_HASH_SEED 131
72
73 constexpr UINT64 func_hash_bkdr_constexpr(const char *str, UINT64 last_value = 0) {
74 return *str ? func_hash_bkdr_constexpr(str + 1, last_value * RT_HASH_SEED + *str) : last_value;
75 }
76
77 // BKDR Hash Function
func_hash_bkdr(const char * str)78 static inline UINT64 func_hash_bkdr(const char *str) {
79 UINT32 seed = RT_HASH_SEED; // 31 131 1313 13131 131313 etc..
80 UINT64 hash = 0;
81
82 while (*str) {
83 hash = hash * RT_HASH_SEED + (*str);
84 *str++; // NOLINT
85 }
86
87 return hash;
88 }
89
90 #define RTSTRING_SWITCH(string) switch (func_hash_bkdr_constexpr(string))
91 #define RTSTRING_CASE(string) case func_hash_bkdr_constexpr(string)
92
93 #ifdef __cplusplus
94 }
95 #endif
96
97 #endif // SRC_RT_BASE_INCLUDE_RT_STRING_UTILS_H_
98