File include/jlib/string.h added (mode: 100644) (index 0000000..5a515bc) |
|
1 |
|
/** |
|
2 |
|
* @file string.h |
|
3 |
|
* @brief carray, string and string_const declaration. |
|
4 |
|
*/ |
|
5 |
|
|
|
6 |
|
#pragma once |
|
7 |
|
|
|
8 |
|
#include "resizable_array.h" |
|
9 |
|
#include <cstring> |
|
10 |
|
|
|
11 |
|
namespace jl |
|
12 |
|
{ |
|
13 |
|
/** |
|
14 |
|
* @brief character array template inherited from rarray. |
|
15 |
|
* |
|
16 |
|
* Made for usage without constructors, destructors, exceptions. |
|
17 |
|
* |
|
18 |
|
* Before using any function, carray must be initialized by any of carray::init functions |
|
19 |
|
* |
|
20 |
|
* @tparam T Type of chars to store in the array. |
|
21 |
|
* |
|
22 |
|
* @see rarray |
|
23 |
|
*/ |
|
24 |
|
template<typename T> |
|
25 |
|
struct carray : rarray<T> |
|
26 |
|
{ |
|
27 |
|
using rarray<T>::init; |
|
28 |
|
/** |
|
29 |
|
* @brief Initialize array with c-style string. |
|
30 |
|
* |
|
31 |
|
* Allocate memory with length of c-style string and copy data from it. |
|
32 |
|
* |
|
33 |
|
* Function must only be called if carray was not initialized by any of carray::init or rarray::init functions. |
|
34 |
|
* |
|
35 |
|
* @param[in] null_terminated_str C-style null-terminated string. |
|
36 |
|
* @return If memory allocation failed, false is returned |
|
37 |
|
* and carray is an undefined state. |
|
38 |
|
*/ |
|
39 |
|
[[nodiscard]] bool init(const char *null_terminated_str) |
|
40 |
|
{ |
|
41 |
|
return init(null_terminated_str, strlen(null_terminated_str)); |
|
42 |
|
} |
|
43 |
|
|
|
44 |
|
/** |
|
45 |
|
* @brief Initialize array with string and given length. |
|
46 |
|
* |
|
47 |
|
* Allocate memory with length of c-style string and copy data from it. |
|
48 |
|
* |
|
49 |
|
* Function must only be called if carray was not initialized by any of carray::init or rarray::init functions. |
|
50 |
|
* |
|
51 |
|
* @param[in] str Pointer to array of at least str_length length. |
|
52 |
|
* @param[in] str_length Length specified by count of chars to copy. |
|
53 |
|
* @return If memory allocation failed, false is returned |
|
54 |
|
* and carray is an undefined state. |
|
55 |
|
*/ |
|
56 |
|
[[nodiscard]] bool init(const T *str, size_t str_length) |
|
57 |
|
{ |
|
58 |
|
if (not this->init(str_length+1)) |
|
59 |
|
return false; |
|
60 |
|
|
|
61 |
|
memcpy(this->p_begin, str, str_length); |
|
62 |
|
this->last() = '\0'; |
|
63 |
|
|
|
64 |
|
return true; |
|
65 |
|
} |
|
66 |
|
|
|
67 |
|
/** |
|
68 |
|
* @brief Initialize array by copying from rarray or carray. |
|
69 |
|
* |
|
70 |
|
* Function must only be called if carray was not initialized by any of carray::init or rarray::init functions. |
|
71 |
|
* |
|
72 |
|
* @param[in] a rarray or carray to copy data from. |
|
73 |
|
* @return If memory allocation failed, false is returned |
|
74 |
|
* and carray is an undefined state. |
|
75 |
|
*/ |
|
76 |
|
[[nodiscard]] bool init(const rarray<const T> &a) |
|
77 |
|
{ |
|
78 |
|
if (not this->init(a.size_allocated())) |
|
79 |
|
return false; |
|
80 |
|
|
|
81 |
|
memcpy(this->begin(), a.begin(), a.size_allocated()); |
|
82 |
|
return true; |
|
83 |
|
} |
|
84 |
|
|
|
85 |
|
/** |
|
86 |
|
* @brief Built read-only access carray by given c-string pointer. |
|
87 |
|
* |
|
88 |
|
* read-only carray cannot be destroyed, so it is safe built. |
|
89 |
|
* |
|
90 |
|
* @param[in] null_terminated_str Pointer to C-style null-terminated string, |
|
91 |
|
* must be valid pointer while carray is in use. |
|
92 |
|
* @return Read-only carray. |
|
93 |
|
*/ |
|
94 |
|
[[nodiscard]] static carray<const T> built_const(const char *null_terminated_str) |
|
95 |
|
{ |
|
96 |
|
carray<const T> ca; |
|
97 |
|
ca.init_move(null_terminated_str, strlen(null_terminated_str)+1); |
|
98 |
|
return ca; |
|
99 |
|
} |
|
100 |
|
|
|
101 |
|
|
|
102 |
|
/// @brief Length of character array, specified by count of chars, excluding null-char '\0'. |
|
103 |
|
[[nodiscard]] inline size_t length() const { return this->count()-1; } |
|
104 |
|
/// @brief Allocated size in bytes for carray. |
|
105 |
|
[[nodiscard]] inline size_t size() const { return this->size_allocated(); } |
|
106 |
|
|
|
107 |
|
[[nodiscard]] inline bool operator == (const carray<T> &a) const |
|
108 |
|
{ |
|
109 |
|
if (this->size() != a.size()) |
|
110 |
|
return false; |
|
111 |
|
return compare(a) == 0; |
|
112 |
|
} |
|
113 |
|
[[nodiscard]] inline bool operator != (const carray<T> &a) const { return !((*this) == a); } |
|
114 |
|
[[nodiscard]] inline bool operator > (const carray<T> &a) const { return compare(a) > 0; } |
|
115 |
|
[[nodiscard]] inline bool operator >= (const carray<T> &a) const { return compare(a) >= 0; } |
|
116 |
|
[[nodiscard]] inline bool operator <= (const carray<T> &a) const { return compare(a) <= 0; } |
|
117 |
|
[[nodiscard]] inline bool operator < (const carray<T> &a) const { return compare(a) < 0; } |
|
118 |
|
|
|
119 |
|
/// @brief Convert to read-only version without issues |
|
120 |
|
operator carray<const T> () const { return carray<const T>{this->operator rarray<const T>()}; } |
|
121 |
|
protected: |
|
122 |
|
/// @brief Same as strcmp, but for carray, instead of const char*. |
|
123 |
|
[[nodiscard]] int compare(const carray<T> &a) const |
|
124 |
|
{ |
|
125 |
|
auto p1 = this->begin(); |
|
126 |
|
auto p2 = a.begin(); |
|
127 |
|
for(; *p1 == *p2; ++p1, ++p2) |
|
128 |
|
if (*p1 == '\0') |
|
129 |
|
return *p2 == '\0' ? 0 : -1; |
|
130 |
|
return *p1 < *p2 ? -1 : 1; |
|
131 |
|
} |
|
132 |
|
}; |
|
133 |
|
|
|
134 |
|
/// @brief Typedef of carray, using char as character type. @see carray |
|
135 |
|
using string = carray<char>; |
|
136 |
|
/// @brief Typedef of read-only carray, using char as character type. @see carray |
|
137 |
|
using string_const = carray<const char>; |
|
138 |
|
} |