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

Commit a2a88cf

Browse files
committed
Add POSIX 2008 string, strings, and locale definitions
It looks like most of POSIX 2008 is missing from Druntime. This particular subset is used by Sociomantic's Ocean, and were ported in an effort to remove duplication.
1 parent aa9ce4f commit a2a88cf

File tree

6 files changed

+268
-0
lines changed

6 files changed

+268
-0
lines changed

mak/COPY

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ COPY=\
205205
$(IMPDIR)\core\sys\posix\iconv.d \
206206
$(IMPDIR)\core\sys\posix\inttypes.d \
207207
$(IMPDIR)\core\sys\posix\libgen.d \
208+
$(IMPDIR)\core\sys\posix\locale.d \
208209
$(IMPDIR)\core\sys\posix\netdb.d \
209210
$(IMPDIR)\core\sys\posix\poll.d \
210211
$(IMPDIR)\core\sys\posix\pthread.d \
@@ -216,6 +217,8 @@ COPY=\
216217
$(IMPDIR)\core\sys\posix\spawn.d \
217218
$(IMPDIR)\core\sys\posix\stdio.d \
218219
$(IMPDIR)\core\sys\posix\stdlib.d \
220+
$(IMPDIR)\core\sys\posix\string.d \
221+
$(IMPDIR)\core\sys\posix\strings.d \
219222
$(IMPDIR)\core\sys\posix\syslog.d \
220223
$(IMPDIR)\core\sys\posix\termios.d \
221224
$(IMPDIR)\core\sys\posix\time.d \

mak/SRCS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ SRCS=\
203203
src\core\sys\posix\iconv.d \
204204
src\core\sys\posix\inttypes.d \
205205
src\core\sys\posix\libgen.d \
206+
src\core\sys\posix\locale.d \
206207
src\core\sys\posix\netdb.d \
207208
src\core\sys\posix\poll.d \
208209
src\core\sys\posix\pthread.d \
@@ -214,6 +215,8 @@ SRCS=\
214215
src\core\sys\posix\spawn.d \
215216
src\core\sys\posix\stdio.d \
216217
src\core\sys\posix\stdlib.d \
218+
src\core\sys\posix\string.d \
219+
src\core\sys\posix\strings.d \
217220
src\core\sys\posix\syslog.d \
218221
src\core\sys\posix\termios.d \
219222
src\core\sys\posix\time.d \

mak/WINDOWS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,9 @@ $(IMPDIR)\core\sys\posix\inttypes.d : src\core\sys\posix\inttypes.d
603603
$(IMPDIR)\core\sys\posix\libgen.d : src\core\sys\posix\libgen.d
604604
copy $** $@
605605

606+
$(IMPDIR)\core\sys\posix\locale.d : src\core\sys\posix\locale.d
607+
copy $** $@
608+
606609
$(IMPDIR)\core\sys\posix\netdb.d : src\core\sys\posix\netdb.d
607610
copy $** $@
608611

@@ -645,6 +648,12 @@ $(IMPDIR)\core\sys\posix\stdio.d : src\core\sys\posix\stdio.d
645648
$(IMPDIR)\core\sys\posix\stdlib.d : src\core\sys\posix\stdlib.d
646649
copy $** $@
647650

651+
$(IMPDIR)\core\sys\posix\string.d : src\core\sys\posix\string.d
652+
copy $** $@
653+
654+
$(IMPDIR)\core\sys\posix\strings.d : src\core\sys\posix\strings.d
655+
copy $** $@
656+
648657
$(IMPDIR)\core\sys\posix\syslog.d : src\core\sys\posix\syslog.d
649658
copy $** $@
650659

src/core/sys/posix/locale.d

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/**
2+
* D header file for POSIX's <locale.h>.
3+
*
4+
* See_Also: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/locale.h.html
5+
* Copyright: D Language Foundation, 2019
6+
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7+
* Authors: Mathias 'Geod24' Lang
8+
* Standards: The Open Group Base Specifications Issue 7, 2018 edition
9+
* Source: $(DRUNTIMESRC core/sys/posix/_locale.d)
10+
*/
11+
module core.sys.posix.locale;
12+
13+
version (Posix):
14+
extern(C):
15+
@system:
16+
nothrow:
17+
@nogc:
18+
19+
version (OSX)
20+
version = OSXBSDLocale;
21+
version (FreeBSD)
22+
version = OSXBSDLocale;
23+
version (NetBSD)
24+
version = OSXBSDLocale;
25+
version (DragonflyBSD)
26+
version = OSXBSDLocale;
27+
28+
29+
///
30+
struct lconv
31+
{
32+
char* currency_symbol;
33+
char* decimal_point;
34+
char frac_digits;
35+
char* grouping;
36+
char* int_curr_symbol;
37+
char int_frac_digits;
38+
char int_n_cs_precedes;
39+
char int_n_sep_by_space;
40+
char int_n_sign_posn;
41+
char int_p_cs_precedes;
42+
char int_p_sep_by_space;
43+
char int_p_sign_posn;
44+
char* mon_decimal_point;
45+
char* mon_grouping;
46+
char* mon_thousands_sep;
47+
char* negative_sign;
48+
char n_cs_precedes;
49+
char n_sep_by_space;
50+
char n_sign_posn;
51+
char* positive_sign;
52+
char p_cs_precedes;
53+
char p_sep_by_space;
54+
char p_sign_posn;
55+
char* thousands_sep;
56+
}
57+
58+
/// Duplicate existing locale
59+
locale_t duplocale(locale_t locale);
60+
/// Free an allocated locale
61+
void freelocale(locale_t locale);
62+
/// Natural language formatting for C
63+
lconv* localeconv();
64+
/// Create a new locale
65+
locale_t newlocale(int mask, const char* locale, locale_t base);
66+
/// Set the C library's notion of natural language formatting style
67+
char* setlocale(int category, const char* locale);
68+
/// Set the per-thread locale
69+
locale_t uselocale (locale_t locale);
70+
71+
version (OSXBSDLocale)
72+
{
73+
///
74+
enum
75+
{
76+
LC_ALL = 0,
77+
LC_COLLATE = 1,
78+
LC_CTYPE = 2,
79+
LC_MESSAGES = 6,
80+
LC_MONETARY = 3,
81+
LC_NUMERIC = 4,
82+
LC_TIME = 5,
83+
}
84+
85+
private struct _xlocale;
86+
87+
///
88+
alias locale_t = _xlocale*;
89+
90+
version (NetBSD)
91+
enum LC_ALL_MASK = (cast(int)~0);
92+
else
93+
enum LC_ALL_MASK = (
94+
LC_COLLATE_MASK | LC_CTYPE_MASK | LC_MESSAGES_MASK |
95+
LC_MONETARY_MASK | LC_NUMERIC_MASK | LC_TIME_MASK);
96+
97+
98+
///
99+
enum
100+
{
101+
LC_COLLATE_MASK = (1 << 0),
102+
LC_CTYPE_MASK = (1 << 1),
103+
LC_MESSAGES_MASK = (1 << 2),
104+
LC_MONETARY_MASK = (1 << 3),
105+
LC_NUMERIC_MASK = (1 << 4),
106+
LC_TIME_MASK = (1 << 5),
107+
}
108+
109+
///
110+
enum LC_GLOBAL_LOCALE = (cast(locale_t)-1);
111+
}
112+
113+
version (linux)
114+
{
115+
///
116+
enum
117+
{
118+
LC_ALL = 6,
119+
LC_COLLATE = 3,
120+
LC_CTYPE = 0,
121+
LC_MESSAGES = 5,
122+
LC_MONETARY = 4,
123+
LC_NUMERIC = 1,
124+
LC_TIME = 2,
125+
126+
// Linux-specific
127+
LC_PAPER = 7,
128+
LC_NAME = 8,
129+
LC_ADDRESS = 9,
130+
LC_TELEPHONE = 10,
131+
LC_MEASUREMENT = 11,
132+
LC_IDENTIFICATION = 12,
133+
}
134+
135+
///
136+
enum
137+
{
138+
LC_ALL_MASK = (LC_CTYPE_MASK | LC_NUMERIC_MASK | LC_TIME_MASK |
139+
LC_COLLATE_MASK | LC_MONETARY_MASK | LC_MESSAGES_MASK |
140+
LC_PAPER_MASK | LC_NAME_MASK | LC_ADDRESS_MASK |
141+
LC_TELEPHONE_MASK | LC_MEASUREMENT_MASK |
142+
LC_IDENTIFICATION_MASK),
143+
144+
LC_COLLATE_MASK = (1 << LC_COLLATE),
145+
LC_CTYPE_MASK = (1 << LC_CTYPE),
146+
LC_MESSAGES_MASK = (1 << LC_MESSAGES),
147+
LC_MONETARY_MASK = (1 << LC_MONETARY),
148+
LC_NUMERIC_MASK = (1 << LC_NUMERIC),
149+
LC_TIME_MASK = (1 << LC_TIME),
150+
151+
// Linux specific
152+
LC_PAPER_MASK = (1 << LC_PAPER),
153+
LC_NAME_MASK = (1 << LC_NAME),
154+
LC_ADDRESS_MASK = (1 << LC_ADDRESS),
155+
LC_TELEPHONE_MASK = (1 << LC_TELEPHONE),
156+
LC_MEASUREMENT_MASK = (1 << LC_MEASUREMENT),
157+
LC_IDENTIFICATION_MASK = (1 << LC_IDENTIFICATION),
158+
}
159+
160+
private struct __locale_struct;
161+
162+
///
163+
alias locale_t = __locale_struct*;
164+
165+
///
166+
enum LC_GLOBAL_LOCALE = (cast(locale_t)-1);
167+
}

src/core/sys/posix/string.d

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* D header file for POSIX's <string.h>.
3+
*
4+
* Note:
5+
* - The <string.h> header shall define NULL and size_t as described in <stddef.h>.
6+
* However, D has builtin `null` and `size_t` is defined in `object`.
7+
*
8+
* See_Also: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/string.h.html
9+
* Copyright: D Language Foundation, 2019
10+
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
11+
* Authors: Mathias 'Geod24' Lang
12+
* Standards: The Open Group Base Specifications Issue 7, 2018 edition
13+
* Source: $(DRUNTIMESRC core/sys/posix/_string.d)
14+
*/
15+
module core.sys.posix.string;
16+
17+
version (Posix):
18+
extern(C):
19+
@system:
20+
nothrow:
21+
@nogc:
22+
23+
/// Exposes `locale_t` as defined in `core.sys.posix.locale` (`<locale.h>`)
24+
public import core.sys.posix.locale : locale_t;
25+
26+
/**
27+
* Exposes the C99 functions
28+
*
29+
* C extensions and XSI extensions are missing
30+
*/
31+
public import core.stdc.string;
32+
33+
/// Copy string until character found
34+
void* memccpy(return void* dst, scope const void* src, int c, size_t n);
35+
/// Copy string (including terminating '\0')
36+
char* stpcpy(return char* dst, scope const char* src);
37+
/// Ditto
38+
char* stpncpy(return char* dst, const char* src, size_t len);
39+
/// Compare strings according to current collation
40+
int strcoll_l(scope const char* s1, scope const char* s2, locale_t locale);
41+
///
42+
char* strerror_l(int, locale_t);
43+
/// Save a copy of a string
44+
char* strndup(scope const char* str, size_t len);
45+
/// Find length of string up to `maxlen`
46+
size_t strnlen(scope const char* str, size_t maxlen);
47+
/// System signal messages
48+
const(char)* strsignal(int);
49+
/// Isolate sequential tokens in a null-terminated string
50+
char* strtok_r(return char* str, scope const char* sep, char** context) pure;
51+
/// Transform a string under locale
52+
size_t strxfrm_l(char* s1, scope const char* s2, size_t n, locale_t locale);

src/core/sys/posix/strings.d

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* D header file for POSIX's <strings.h>.
3+
*
4+
* Note: Do not mistake this module for <string.h> (singular),
5+
* available at `core.sys.posix.string`.
6+
*
7+
* See_Also: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/strings.h.html
8+
* Copyright: D Language Foundation, 2019
9+
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
10+
* Authors: Mathias 'Geod24' Lang
11+
* Standards: The Open Group Base Specifications Issue 7, 2018 edition
12+
* Source: $(DRUNTIMESRC core/sys/posix/_strings.d)
13+
*/
14+
module core.sys.posix.strings;
15+
16+
version (Posix):
17+
extern(C):
18+
@system:
19+
nothrow:
20+
@nogc:
21+
22+
///
23+
public import core.sys.posix.locale : locale_t;
24+
25+
/// Find first bit set in a word
26+
int ffs(int i) @safe pure;
27+
/// Compare two strings ignoring case
28+
int strcasecmp(scope const char* s1, scope const char* s2);
29+
/// Compare two strings ignoring case, with the specified locale
30+
int strcasecmp_l(scope const char* s1, scope const char* s2, scope locale_t locale);
31+
/// Compare two strings ignoring case, up to n characters
32+
int strncasecmp(scope const char* s1, scope const char* s2, size_t n);
33+
/// Compare two strings ignoring case, with the specified locale, up to n characters
34+
int strncasecmp_l(scope const char* s1, const char* s2, size_t n, locale_t locale);

0 commit comments

Comments
 (0)