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