1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
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 Jiliang.ljl |
17 | * \date Mar 2021 |
18 | * \brief Alphameric interface |
19 | * \detail Alphameric is used to facilitate string concatenation |
20 | * memory allocation. |
21 | */ |
22 | |
23 | #ifndef __AILEGO_STRING_STRING_CONCAT_HELPER_H__ |
24 | #define __AILEGO_STRING_STRING_CONCAT_HELPER_H__ |
25 | |
26 | #include <cstdlib> |
27 | #include <vector> |
28 | #include "string_view.h" |
29 | |
30 | namespace ailego { |
31 | namespace internal { |
32 | |
33 | //! Helper class to convert integer and float types to string, facilitating |
34 | //! string concatenation memory allocation. |
35 | class Alphameric { |
36 | public: |
37 | //! Deals with int, int8_t, int16_t, int32_t, bool, short, signed char, non |
38 | //! class enum |
39 | Alphameric(int n) |
40 | : view_(buffer_, snprintf(buffer_, sizeof(buffer_), "%d" , n)) {} |
41 | |
42 | //! Deals with unsigned int, uint8_t, uint16_t, uint32_t, unsigned short, |
43 | //! unsigned char |
44 | Alphameric(unsigned int n) |
45 | : view_(buffer_, snprintf(buffer_, sizeof(buffer_), "%u" , n)) {} |
46 | |
47 | //! Deals with long, int32_t, int64_t |
48 | Alphameric(long n) |
49 | : view_(buffer_, snprintf(buffer_, sizeof(buffer_), "%ld" , n)) {} |
50 | |
51 | //! Deals with unsigned long, uint32_t, uint64_t |
52 | Alphameric(unsigned long n) |
53 | : view_(buffer_, snprintf(buffer_, sizeof(buffer_), "%lu" , n)) {} |
54 | |
55 | //! Deals with long long, int64_t |
56 | Alphameric(long long n) |
57 | : view_(buffer_, snprintf(buffer_, sizeof(buffer_), "%lld" , n)) {} |
58 | |
59 | //! Deals with unsigned long long, uint64_t |
60 | Alphameric(unsigned long long n) |
61 | : view_(buffer_, snprintf(buffer_, sizeof(buffer_), "%llu" , n)) {} |
62 | |
63 | //! Deals with float, with 6 precision digit the same as std::to_string |
64 | Alphameric(float f) |
65 | : view_(buffer_, snprintf(buffer_, sizeof(buffer_), "%g" , f)) {} |
66 | |
67 | //! Deals with double, with 6 precision digit the same as std::to_string |
68 | Alphameric(double f) |
69 | : view_(buffer_, snprintf(buffer_, sizeof(buffer_), "%g" , f)) {} |
70 | |
71 | //! Deals with long double, with 6 precision digit the same as std::to_string |
72 | Alphameric(long double f) |
73 | : view_(buffer_, snprintf(buffer_, sizeof(buffer_), "%Lg" , f)) {} |
74 | |
75 | //! Deals with const char* |
76 | Alphameric(const char *s) : view_(s) {} |
77 | |
78 | //! Deals with std::string |
79 | Alphameric(const std::string &s) : view_(s) {} |
80 | |
81 | //! Deals with StringView |
82 | Alphameric(StringView s) : view_(s) {} |
83 | |
84 | // Use string literals ":" instead of character literals ':'. |
85 | Alphameric(char c) = delete; |
86 | Alphameric(const Alphameric &) = delete; |
87 | Alphameric &operator=(const Alphameric &) = delete; |
88 | |
89 | //! Deals with enum class with non int underlying type |
90 | template <typename T, |
91 | typename = typename std::enable_if< |
92 | std::is_enum<T>{} && !std::is_convertible<T, int>{}>::type> |
93 | Alphameric(T e) |
94 | : Alphameric(static_cast<typename std::underlying_type<T>::type>(e)) {} |
95 | |
96 | //! Deals with std::vector<bool> subscript reference |
97 | template <typename T, |
98 | typename std::enable_if< |
99 | std::is_class<T>::value && |
100 | (std::is_same<T, std::vector<bool>::reference>::value || |
101 | std::is_same<T, std::vector<bool>::const_reference>::value)>:: |
102 | type * = nullptr> |
103 | Alphameric(T e) : Alphameric(static_cast<bool>(e)) {} |
104 | |
105 | //! string size |
106 | size_t size() const { |
107 | return view_.size(); |
108 | } |
109 | |
110 | //! string data |
111 | const char *data() const { |
112 | return view_.data(); |
113 | } |
114 | |
115 | //! string view |
116 | StringView view() const { |
117 | return view_; |
118 | } |
119 | |
120 | private: |
121 | static constexpr int kBufferSize = 32; |
122 | char buffer_[kBufferSize]; |
123 | StringView view_; |
124 | }; |
125 | |
126 | |
127 | } // namespace internal |
128 | } // namespace ailego |
129 | |
130 | #endif // __AILEGO_STRING_STRING_CONCAT_HELPER_H__ |
131 | |