Cuda battery library
Loading...
Searching...
No Matches
string.hpp
Go to the documentation of this file.
1// Copyright 2021 Pierre Talbot
2
3#ifndef CUDA_BATTERY_STRING_HPP
4#define CUDA_BATTERY_STRING_HPP
5
6#include <string>
7#include "utility.hpp"
8#include "vector.hpp"
9
10namespace battery {
11
12/** `string` represents a fixed sized array of characters based on `vector<char>`.
13 All strings are null-terminated. */
14template<class Allocator=standard_allocator>
15class string {
17public:
19 using allocator_type = Allocator;
20 using value_type = char;
21
22 template <class Alloc2>
23 friend class string;
24
25 /** Allocate a string of size `n` using `allocator`. */
26 CUDA string(size_t n, const allocator_type& alloc = allocator_type()):
27 data_(n+1, alloc) /* +1 for null-termination */
28 {
29 data_[n] = '\0'; // In case the user modifies the string.
30 }
31
32 CUDA string(const allocator_type& alloc = allocator_type()): string((size_t)0, alloc) {}
33
34 /** Allocate a string from `raw_string` using `allocator`. */
35 CUDA string(const char* raw_string, const allocator_type& alloc = allocator_type()):
36 data_(raw_string, strlen(raw_string)+1, alloc) {}
37
38 /** Copy constructor with an allocator. */
39 template <class Allocator2>
41 data_(other.data_, alloc) {}
42
43 /** Redefine the copy constructor to be sure it calls a constructor with an allocator. */
45
46 string(string<allocator_type>&& other) = default;
48 data_ = other.data_;
49 return *this;
50 }
51
52 string(const std::string& other, const allocator_type& alloc = allocator_type()):
53 data_(other.data(), other.size()+1, alloc) {}
54
55 CUDA allocator_type get_allocator() const { return data_.get_allocator(); }
56 CUDA size_t size() const { return data_.size() == 0 ? 0 : (data_.size() - 1); }
57 CUDA char& operator[](size_t i) { assert(i < size()); return data_[i]; }
58 CUDA const char& operator[](size_t i) const { assert(i < size()); return data_[i]; }
59
60 CUDA char* data() { return data_.data(); }
61 CUDA const char* data() const { return data_.data(); }
62
63 CUDA NI void print() const {
64 printf("%s", data());
65 }
66
67 CUDA NI bool ends_with(const char* suffix) const {
68 size_t suffix_len = strlen(suffix);
69 return battery::strcmp(data() + size() - suffix_len, suffix) == 0;
70 }
71
72 template <class IntegerType>
73 CUDA NI static this_type from_int(IntegerType x, const allocator_type& alloc = allocator_type()) {
74 if(x == 0) { return this_type("0", alloc); }
75 size_t s = 0;
76 bool neg = x < IntegerType{0};
77 if(neg) {
78 x = -x;
79 s++;
80 }
81 for(size_t y = x; y > 0; y = y / 10, ++s) {}
82 this_type buffer(s, alloc);
83 if(neg) {
84 buffer[0] = '-';
85 }
86 for(size_t i = s-1; x > 0; --i) {
87 buffer[i] = '0' + (x % 10);
88 x = x / 10;
89 }
90 return std::move(buffer);
91 }
92
93
94 template<class Alloc1, class Alloc2>
95 CUDA friend bool operator==(const string<Alloc1>& lhs, const string<Alloc2>& rhs);
96};
97
98namespace impl {
99 template<class Allocator>
100 CUDA NI string<Allocator> concat(const char* lhs, size_t lhs_len, const char* rhs, size_t rhs_len, const Allocator& alloc) {
101 string<Allocator> res(lhs_len + rhs_len, alloc);
102 size_t k = 0;
103 for(size_t i = 0; i < lhs_len; ++i, ++k) { res[k] = lhs[i]; }
104 for(size_t i = 0; i < rhs_len; ++i, ++k) { res[k] = rhs[i]; }
105 return std::move(res);
106 }
107}
108
109template<class Alloc1, class Alloc2>
110CUDA bool operator==(const string<Alloc1>& lhs, const string<Alloc2>& rhs) {
111 return lhs.size() == rhs.size() && battery::strcmp(lhs.data(), rhs.data()) == 0;
112}
113
114template<class Allocator>
115CUDA bool operator==(const char* lhs, const string<Allocator>& rhs) {
116 return battery::strcmp(lhs, rhs.data()) == 0;
117}
118
119template<class Allocator>
120CUDA bool operator==(const string<Allocator>& lhs, const char* rhs) {
121 return battery::strcmp(lhs.data(), rhs) == 0;
122}
123
124template<class Alloc1, class Alloc2>
125CUDA bool operator!=(const string<Alloc1>& lhs, const string<Alloc2>& rhs) {
126 return !(lhs == rhs);
127}
128
129template<class Allocator>
130CUDA bool operator!=(const char* lhs, const string<Allocator>& rhs) {
131 return !(lhs == rhs);
132}
133
134template<class Allocator>
135CUDA bool operator!=(const string<Allocator>& lhs, const char* rhs) {
136 return !(lhs == rhs);
137}
138
139template<class Allocator>
141 return impl::concat(lhs.data(), lhs.size(), rhs.data(), rhs.size(), lhs.get_allocator());
142}
143
144template<class Allocator>
145CUDA string<Allocator> operator+(const char* lhs, const string<Allocator>& rhs) {
146 return impl::concat(lhs, strlen(lhs), rhs.data(), rhs.size(), rhs.get_allocator());
147}
148
149template<class Allocator>
150CUDA string<Allocator> operator+(const string<Allocator>& lhs, const char* rhs) {
151 return impl::concat(lhs.data(), lhs.size(), rhs, strlen(rhs), lhs.get_allocator());
152}
153
154} // namespace battery
155
156#endif
Definition string.hpp:15
string(string< allocator_type > &&other)=default
CUDA NI bool ends_with(const char *suffix) const
Definition string.hpp:67
CUDA size_t size() const
Definition string.hpp:56
CUDA char * data()
Definition string.hpp:60
CUDA allocator_type get_allocator() const
Definition string.hpp:55
CUDA static NI this_type from_int(IntegerType x, const allocator_type &alloc=allocator_type())
Definition string.hpp:73
CUDA char & operator[](size_t i)
Definition string.hpp:57
CUDA const char & operator[](size_t i) const
Definition string.hpp:58
string(const std::string &other, const allocator_type &alloc=allocator_type())
Definition string.hpp:52
CUDA string(const allocator_type &alloc=allocator_type())
Definition string.hpp:32
CUDA const char * data() const
Definition string.hpp:61
CUDA string(size_t n, const allocator_type &alloc=allocator_type())
Definition string.hpp:26
CUDA string< allocator_type > & operator=(string< allocator_type > other)
Definition string.hpp:47
CUDA string(const string< allocator_type > &other)
Definition string.hpp:44
CUDA friend bool operator==(const string< Alloc1 > &lhs, const string< Alloc2 > &rhs)
Definition string.hpp:110
Allocator allocator_type
Definition string.hpp:19
CUDA string(const char *raw_string, const allocator_type &alloc=allocator_type())
Definition string.hpp:35
char value_type
Definition string.hpp:20
CUDA NI void print() const
Definition string.hpp:63
string< Allocator > this_type
Definition string.hpp:18
CUDA string(const string< Allocator2 > &other, const allocator_type &alloc=allocator_type())
Definition string.hpp:40
Definition vector.hpp:20
CUDA allocator_type get_allocator() const
Definition vector.hpp:214
CUDA size_t size() const
Definition vector.hpp:235
CUDA value_type * data()
Definition vector.hpp:232
Definition algorithm.hpp:10
CUDA string< Allocator > operator+(const string< Allocator > &lhs, const string< Allocator > &rhs)
Definition string.hpp:140
CUDA size_t strlen(const char *str)
Definition utility.hpp:99
CUDA int strcmp(const char *s1, const char *s2)
Definition utility.hpp:108
CUDA bool operator==(const string< Alloc1 > &lhs, const string< Alloc2 > &rhs)
Definition string.hpp:110
#define CUDA
Definition utility.hpp:59
#define NI
Definition utility.hpp:62