P99
p99_uchar.h
Go to the documentation of this file.
1 /* This may look like nonsense, but it really is -*- mode: C; coding: utf-8 -*- */
2 /* */
3 /* Except for parts copied from previous work and as explicitly stated below, */
4 /* the author and copyright holder for this work is */
5 /* (C) copyright 2017 Jens Gustedt, INRIA, France */
6 /* */
7 /* This file is free software; it is part of the P99 project. */
8 /* */
9 /* Licensed under the Apache License, Version 2.0 (the "License"); */
10 /* you may not use this file except in compliance with the License. */
11 /* You may obtain a copy of the License at */
12 /* */
13 /* http://www.apache.org/licenses/LICENSE-2.0 */
14 /* */
15 /* Unless required by applicable law or agreed to in writing, software */
16 /* distributed under the License is distributed on an "AS IS" BASIS, */
17 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
18 /* See the License for the specific language governing permissions and */
19 /* limitations under the License. */
20 /* */
26 #ifndef P99_uchar_H_
27 # define P99_uchar_H_
28 
29 # include "p99_compiler.h"
30 # include "p99_int.h"
31 # include <wchar.h>
32 
33 typedef uint_least16_t char16_t;
34 typedef uint_least32_t char32_t;
35 
36 _Static_assert(sizeof(L"äß")==sizeof(U"äß"), "we need to wchar_t to represend unicode for this to work");
37 
38 union p00_mbstate {
39  unsigned char p00_bytes[sizeof(mbstate_t)];
40  mbstate_t p00_state;
41 };
42 
43 P99_WEAK(mbrtoc16)
44 size_t mbrtoc16(char16_t * restrict _pc16,
45  const char * restrict _s, size_t _n,
46  mbstate_t * restrict _ps) {
47  static union p00_mbstate p00_state = { .p00_bytes = { 0 }, };
48  if (!_ps) _ps = &p00_state.p00_state;
49  if (!_s) {
50  _pc16 = 0;
51  _s = "";
52  _n = 1;
53  }
54  wchar_t p00_wc;
55 # if WCHAR_MAX > 0xFFFF
56  mbstate_t p00_st = *_ps;
57  size_t p00_ret = mbrtowc(&p00_wc, _s, _n, &p00_st);
58  if (p00_wc > 0xFFFF) return -1;
59  else *_ps = p00_st;
60 # else
61  size_t p00_ret = mbrtowc(&p00_wc, _s, _n, _ps);
62 # endif
63  if (_pc16 && (p00_ret <= _n)) *_pc16 = p00_wc;
64  return p00_ret;
65 }
66 
67 P99_WEAK(c16rtomb)
68 size_t c16rtomb(char * restrict _s, char16_t _c16,
69  mbstate_t * restrict _ps) {
70  static union p00_mbstate p00_state = { .p00_bytes = { 0 }, };
71  if (!_ps) _ps = &p00_state.p00_state;
72  return wcrtomb(_s, _c16, _ps);
73 }
74 
75 P99_WEAK(mbrtoc32)
76 size_t mbrtoc32(char32_t * restrict _pc32,
77  const char * restrict _s, size_t _n,
78  mbstate_t * restrict _ps) {
79  static union p00_mbstate p00_state = { .p00_bytes = { 0 }, };
80  if (!_ps) _ps = &p00_state.p00_state;
81  if (!_s) {
82  _pc32 = 0;
83  _s = "";
84  _n = 1;
85  }
86  wchar_t p00_wc;
87 # if WCHAR_MAX > 0xFFFFFFFF
88  mbstate_t p00_st = *_ps;
89  size_t p00_ret = mbrtowc(&p00_wc, _s, _n, &p00_st);
90  if (p00_wc > 0xFFFFFFFF) return -1;
91  else *_ps = p00_st;
92 # else
93  size_t p00_ret = mbrtowc(&p00_wc, _s, _n, _ps);
94 # endif
95  if (_pc32 && (p00_ret <= _n)) *_pc32 = p00_wc;
96  return p00_ret;
97 }
98 
99 P99_WEAK(c32rtomb)
100 size_t c32rtomb(char * restrict _s, char32_t _c32,
101  mbstate_t * restrict _ps) {
102  if (_s && (_c32 > WCHAR_MAX)) {
103  errno = EILSEQ;
104  return -1;
105  }
106  static union p00_mbstate p00_state = { .p00_bytes = { 0 }, };
107  if (!_ps) _ps = &p00_state.p00_state;
108  return wcrtomb(_s, _c32, _ps);
109 }
110 
111 
112 
113 #endif
char16_t
uint_least16_t char16_t
Definition: p99_uchar.h:33
_Static_assert
_Static_assert(sizeof(L"äß")==sizeof(U"äß"), "we need to wchar_t to represend unicode for this to work")
p99_compiler.h
Group compiler dependencies together in one file.
char32_t
uint_least32_t char32_t
Definition: p99_uchar.h:34
P99_WEAK
#define P99_WEAK(...)
Declare a symbol to be weak such that it can be provided several times without error.
Definition: p99_compiler.h:561
char32_t
uint_least32_t char32_t
Definition: p99_libc.h:221
char16_t
uint_least16_t char16_t
Definition: p99_libc.h:220
errno
errno
Definition: p99_constraint.h:199
p99_int.h
Macros handling integer types and initialization.