Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 032b970

Browse files
gchateletTurkeyMan
authored andcommitted
First implementation of std::string/std::vector
1 parent 07e0f00 commit 032b970

File tree

6 files changed

+526
-0
lines changed

6 files changed

+526
-0
lines changed

mak/COPY

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ COPY=\
5050
\
5151
$(IMPDIR)\core\stdcpp\typeinfo.d \
5252
$(IMPDIR)\core\stdcpp\exception.d \
53+
$(IMPDIR)\core\stdcpp\allocator.d \
54+
$(IMPDIR)\core\stdcpp\string.d \
55+
$(IMPDIR)\core\stdcpp\vector.d \
5356
\
5457
$(IMPDIR)\core\sys\darwin\execinfo.d \
5558
$(IMPDIR)\core\sys\darwin\pthread.d \

mak/DOCS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ DOCS=\
4040
\
4141
$(DOCDIR)\core_stdcpp_exception.html \
4242
$(DOCDIR)\core_stdcpp_typeinfo.html \
43+
$(DOCDIR)\core_stdcpp_allocator.html \
44+
$(DOCDIR)\core_stdcpp_string.html \
45+
$(DOCDIR)\core_stdcpp_vector.html \
4346
\
4447
$(DOCDIR)\core_sync_barrier.html \
4548
$(DOCDIR)\core_sync_condition.html \

mak/SRCS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ SRCS=\
4646
src\core\stdc\time.d \
4747
src\core\stdc\wchar_.d \
4848
\
49+
src\core\stdcpp\allocator.d \
50+
src\core\stdcpp\string.d \
51+
src\core\stdcpp\vector.d \
52+
\
4953
src\core\sync\barrier.d \
5054
src\core\sync\condition.d \
5155
src\core\sync\config.d \

src/core/stdcpp/allocator.d

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* D header file for interaction with C++ std::allocator.
3+
*
4+
* Copyright: Copyright Guillaume Chatelet 2014 - 2015.
5+
* License: Distributed under the
6+
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7+
* (See accompanying file LICENSE)
8+
* Authors: Guillaume Chatelet
9+
* Source: $(DRUNTIMESRC core/stdcpp/allocator.d)
10+
*/
11+
12+
module core.stdcpp.allocator;
13+
14+
extern(C++, std):
15+
16+
/**
17+
* Allocators are classes that define memory models to be used by some parts of
18+
* the C++ Standard Library, and most specifically, by STL containers.
19+
*/
20+
struct allocator(T) { }

src/core/stdcpp/string.d

Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
1+
/**
2+
* D header file for interaction with C++ std::string.
3+
*
4+
* Copyright: Copyright Guillaume Chatelet 2014 - 2015.
5+
* License: Distributed under the
6+
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7+
* (See accompanying file LICENSE)
8+
* Authors: Guillaume Chatelet
9+
* Source: $(DRUNTIMESRC core/stdcpp/string.d)
10+
*/
11+
12+
module core.stdcpp.string;
13+
14+
/**
15+
* Because of broken name mangling of extern C++, this file is only available
16+
* for Linux platform right now. Name mangling is hardcoded with pragmas.
17+
*/
18+
version(linux):
19+
pragma(lib, "stdc++");
20+
21+
///////////////////////////////////////////////////////////////////////////////
22+
// std::string declaration.
23+
//
24+
// Current caveats :
25+
// - manual name mangling (=> only string is functionnal, wstring won't work)
26+
// https://issues.dlang.org/show_bug.cgi?id=14086
27+
// https://issues.dlang.org/show_bug.cgi?id=14178
28+
// - won't work with custom allocators.
29+
// - iterators are implemented as pointers
30+
// - no reverse_iterator nor rbegin/rend
31+
// - missing functions : replace, swap
32+
///////////////////////////////////////////////////////////////////////////////
33+
34+
import core.stdcpp.allocator;
35+
36+
extern(C++, std):
37+
38+
/**
39+
* Character traits classes specify character properties and provide specific
40+
* semantics for certain operations on characters and sequences of characters.
41+
*/
42+
struct char_traits(CharT) {}
43+
44+
/**
45+
* The basic_string is the generalization of class string for any character
46+
* type.
47+
*/
48+
struct basic_string(T, TRAITS = char_traits!T, ALLOC = allocator!T)
49+
{
50+
enum size_t npos = size_t.max;
51+
52+
alias value_type = T;
53+
alias traits_type = TRAITS;
54+
alias allocator_type = ALLOC;
55+
alias reference = ref T;
56+
alias const_reference = ref const(T);
57+
alias pointer = T*;
58+
alias const_pointer = const(T*);
59+
alias iterator = pointer;
60+
alias const_iterator = const_pointer;
61+
// alias reverse_iterator
62+
// alias const_reverse_iterator
63+
alias difference_type = ptrdiff_t;
64+
alias size_type = size_t;
65+
66+
// Ctor/dtor
67+
pragma(mangle, "_ZNSsC1Ev")
68+
@disable this();
69+
70+
pragma(mangle, "_ZNSsC1ERKSs")
71+
this(ref const this);
72+
73+
pragma(mangle, "_ZNSsC1EPKcRKSaIcE")
74+
this(const(T*) _, ref const allocator_type _ = defaultAlloc);
75+
76+
pragma(mangle, "_ZNSsD1Ev")
77+
~this();
78+
79+
pragma(mangle, "_ZNSsaSERKSs")
80+
ref basic_string opAssign(ref const basic_string s);
81+
82+
// Iterators
83+
pragma(mangle, "_ZNSs5beginEv")
84+
iterator begin() nothrow;
85+
86+
pragma(mangle, "_ZNKSs5beginEv")
87+
const_iterator begin() nothrow const;
88+
89+
pragma(mangle, "_ZNKSs6cbeginEv")
90+
const_iterator cbegin() nothrow const;
91+
92+
pragma(mangle, "_ZNSs3endEv")
93+
iterator end() nothrow;
94+
95+
pragma(mangle, "_ZNKSs3endEv")
96+
const_iterator end() nothrow const;
97+
98+
pragma(mangle, "_ZNKSs4cendEv")
99+
const_iterator cend() nothrow const;
100+
101+
// no reverse iterator for now.
102+
103+
// Capacity
104+
pragma(mangle, "_ZNKSs4sizeEv")
105+
size_t size() nothrow const;
106+
107+
pragma(mangle, "_ZNKSs6lengthEv")
108+
size_t length() nothrow const;
109+
110+
pragma(mangle, "_ZNKSs8max_sizeEv")
111+
size_t max_size() nothrow const;
112+
113+
pragma(mangle, "_ZNKSs8capacityEv")
114+
size_t capacity() nothrow const;
115+
116+
pragma(mangle, "_ZNKSs5emptyEv")
117+
bool empty() nothrow const;
118+
119+
pragma(mangle, "_ZNSs5clearEv")
120+
void clear() nothrow;
121+
122+
pragma(mangle, "_ZNSs6resizeEm")
123+
void resize(size_t n);
124+
125+
pragma(mangle, "_ZNSs6resizeEmc")
126+
void resize(size_t n, T c);
127+
128+
pragma(mangle, "_ZNSs7reserveEm")
129+
void reserve(size_t n = 0);
130+
131+
pragma(mangle, "_ZNSs13shrink_to_fitEv")
132+
void shrink_to_fit();
133+
134+
// Element access
135+
pragma(mangle, "_ZNSsixEm")
136+
ref T opIndex(size_t i);
137+
138+
pragma(mangle, "_ZNKSsixEm")
139+
ref const(T) opIndex(size_t i) const;
140+
141+
pragma(mangle, "_ZNSs2atEm")
142+
ref T at(size_t i);
143+
144+
pragma(mangle, "_ZNKSs2atEm")
145+
ref const(T) at(size_t i) const;
146+
147+
pragma(mangle, "_ZNSs4backEv")
148+
ref T back();
149+
150+
pragma(mangle, "_ZNKSs4backEv")
151+
ref const(T) back() const;
152+
153+
pragma(mangle, "_ZNSs5frontEv")
154+
ref T front();
155+
156+
pragma(mangle, "_ZNKSs5frontEv")
157+
ref const(T) front() const;
158+
159+
// Modifiers
160+
pragma(mangle, "_ZNSspLERKSs")
161+
ref basic_string opOpAssign(string op)(ref const basic_string _) if (op == "+");
162+
163+
pragma(mangle, "_ZNSspLEPKc")
164+
ref basic_string opOpAssign(string op)(const(T*) _) if (op == "+");
165+
166+
pragma(mangle, "_ZNSspLEc")
167+
ref basic_string opOpAssign(string op)(T _) if (op == "+");
168+
169+
pragma(mangle, "_ZNSs6appendEmc")
170+
ref basic_string append(size_t n, char c);
171+
172+
pragma(mangle, "_ZNSs6appendEPKc")
173+
ref basic_string append(const char* s);
174+
175+
pragma(mangle, "_ZNSs6appendEPKcm")
176+
ref basic_string append(const char* s, size_t n);
177+
178+
pragma(mangle, "_ZNSs6appendERKSs")
179+
ref basic_string append(ref const basic_string str);
180+
181+
pragma(mangle, "_ZNSs6appendERKSsmm")
182+
ref basic_string append(ref const basic_string str, size_t subpos, size_t sublen);
183+
184+
pragma(mangle, "_ZNSs9push_backEc")
185+
void push_back(T c);
186+
187+
pragma(mangle, "_ZNSs6assignEmc")
188+
ref basic_string assign(size_t n, char c);
189+
190+
pragma(mangle, "_ZNSs6assignEPKc")
191+
ref basic_string assign(const char* s);
192+
193+
pragma(mangle, "_ZNSs6assignEPKcm")
194+
ref basic_string assign(const char* s, size_t n);
195+
196+
pragma(mangle, "_ZNSs6assignERKSs")
197+
ref basic_string assign(ref const basic_string str);
198+
199+
pragma(mangle, "_ZNSs6assignERKSsmm")
200+
ref basic_string assign(ref const basic_string str, size_t subpos, size_t sublen);
201+
202+
pragma(mangle, "_ZNSs6insertEmRKSs")
203+
ref basic_string insert (size_t pos, ref const basic_string str);
204+
205+
pragma(mangle, "_ZNSs6insertEmRKSsmm")
206+
ref basic_string insert (size_t pos, ref const basic_string str, size_t subpos, size_t sublen);
207+
208+
pragma(mangle, "_ZNSs6insertEmPKc")
209+
ref basic_string insert (size_t pos, const char* s);
210+
211+
pragma(mangle, "_ZNSs6insertEmPKcm")
212+
ref basic_string insert (size_t pos, const char* s, size_t n);
213+
214+
pragma(mangle, "_ZNSs6insertEmmc")
215+
ref basic_string insert (size_t pos, size_t n, char c);
216+
217+
pragma(mangle, "_ZNSs5eraseEmm")
218+
ref basic_string erase(size_t pos = 0, size_t len = npos);
219+
220+
// replace
221+
// swap
222+
pragma(mangle, "_ZNSs8pop_backEv")
223+
void pop_back();
224+
225+
// String operations
226+
pragma(mangle, "_ZNKSs5c_strEv")
227+
const(T*) c_str() nothrow const;
228+
229+
pragma(mangle, "_ZNKSs4dataEv")
230+
const(T*) data() nothrow const;
231+
232+
pragma(mangle, "_ZNKSs4copyEPcmm")
233+
size_t copy(T* s, size_t len, size_t pos = 0) const;
234+
235+
pragma(mangle, "_ZNKSs4findERKSsm")
236+
size_t find(ref const basic_string str, size_t pos = 0) nothrow const;
237+
238+
pragma(mangle, "_ZNKSs4findEPKcm")
239+
size_t find(const(T*) s, size_t pos = 0) const;
240+
241+
pragma(mangle, "_ZNKSs4findEPKcmm")
242+
size_t find(const(T*) s, size_t pos, size_type n) const;
243+
244+
pragma(mangle, "_ZNKSs4findEcm")
245+
size_t find(T c, size_t pos = 0) nothrow const;
246+
247+
pragma(mangle, "_ZNKSs5rfindERKSsm")
248+
size_t rfind(ref const basic_string str, size_t pos = npos) nothrow const;
249+
250+
pragma(mangle, "_ZNKSs5rfindEPKcm")
251+
size_t rfind(const(T*) s, size_t pos = npos) const;
252+
253+
pragma(mangle, "_ZNKSs5rfindEPKcmm")
254+
size_t rfind(const(T*) s, size_t pos, size_t n) const;
255+
256+
pragma(mangle, "_ZNKSs5rfindEcm")
257+
size_t rfind(T c, size_t pos = npos) nothrow const;
258+
259+
pragma(mangle, "_ZNKSs13find_first_ofERKSsm")
260+
size_t find_first_of(ref const basic_string str, size_t pos = 0) nothrow const;
261+
262+
pragma(mangle, "_ZNKSs13find_first_ofEPKcm")
263+
size_t find_first_of(const(T*) s, size_t pos = 0) const;
264+
265+
pragma(mangle, "_ZNKSs13find_first_ofEPKcmm")
266+
size_t find_first_of(const(T*) s, size_t pos, size_t n) const;
267+
268+
pragma(mangle, "_ZNKSs13find_first_ofEcm")
269+
size_t find_first_of(T c, size_t pos = 0) nothrow const;
270+
271+
pragma(mangle, "_ZNKSs12find_last_ofERKSsm")
272+
size_t find_last_of(ref const basic_string str, size_t pos = npos) nothrow const;
273+
274+
pragma(mangle, "_ZNKSs12find_last_ofEPKcm")
275+
size_t find_last_of(const(T*) s, size_t pos = npos) const;
276+
277+
pragma(mangle, "_ZNKSs12find_last_ofEPKcmm")
278+
size_t find_last_of(const(T*) s, size_t pos, size_t n) const;
279+
280+
pragma(mangle, "_ZNKSs12find_last_ofEcm")
281+
size_t find_last_of(T c, size_t pos = npos) nothrow const;
282+
283+
pragma(mangle, "_ZNKSs17find_first_not_ofERKSsm")
284+
size_t find_first_not_of(ref const basic_string str, size_t pos = 0) nothrow const;
285+
286+
pragma(mangle, "_ZNKSs17find_first_not_ofEPKcm")
287+
size_t find_first_not_of(const(T*) s, size_t pos = 0) const;
288+
289+
pragma(mangle, "_ZNKSs17find_first_not_ofEPKcmm")
290+
size_t find_first_not_of(const(T*) s, size_t pos, size_t n) const;
291+
292+
pragma(mangle, "_ZNKSs17find_first_not_ofEcm")
293+
size_t find_first_not_of(T c, size_t pos = 0) nothrow const;
294+
295+
pragma(mangle, "_ZNKSs16find_last_not_ofERKSsm")
296+
size_t find_last_not_of(ref const basic_string str, size_t pos = npos) nothrow const;
297+
298+
pragma(mangle, "_ZNKSs16find_last_not_ofEPKcm")
299+
size_t find_last_not_of(const(T*) s, size_t pos = npos) const;
300+
301+
pragma(mangle, "_ZNKSs16find_last_not_ofEPKcmm")
302+
size_t find_last_not_of(const(T*) s, size_t pos, size_t n) const;
303+
304+
pragma(mangle, "_ZNKSs16find_last_not_ofEcm")
305+
size_t find_last_not_of(T c, size_t pos = npos) nothrow const;
306+
307+
pragma(mangle, "_ZNKSs6substrEmm")
308+
basic_string substr(size_t pos = 0, size_t len = npos) const;
309+
310+
pragma(mangle, "_ZNKSs7compareERKSs")
311+
int compare(ref const basic_string str) nothrow const;
312+
313+
pragma(mangle, "_ZNKSs7compareEmmRKSs")
314+
int compare(size_t pos, size_t len, ref const basic_string str) const;
315+
316+
pragma(mangle, "_ZNKSs7compareEmmRKSsmm")
317+
int compare(size_t pos, size_t len, ref const basic_string str, size_t subpos, size_t sublen) const;
318+
319+
pragma(mangle, "_ZNKSs7compareEPKc")
320+
int compare(const(T*) s) const;
321+
322+
pragma(mangle, "_ZNKSs7compareEmmPKc")
323+
int compare(size_t pos, size_t len, const(T*) s) const;
324+
325+
pragma(mangle, "_ZNKSs7compareEmmPKcm")
326+
int compare(size_t pos, size_t len, const(T*) s, size_t n) const;
327+
328+
// D helpers
329+
const(T[]) asArray() const { return c_str()[0 .. size()]; }
330+
331+
private:
332+
void[8] _ = void; // to match sizeof(std::string) and pad the object correctly.
333+
__gshared static immutable allocator!T defaultAlloc;
334+
}
335+
336+
alias basic_string!char std_string;
337+
alias basic_string!wchar std_wstring;

0 commit comments

Comments
 (0)