source: mainline/common/stdc/wchar.c

Last change on this file was f94a11f, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 months ago

Rename mbstate_t field to prevent confusion with continuation bytes

  • Property mode set to 100644
File size: 2.6 KB
RevLine 
[16bfcd3]1/*
[696b405]2 * Copyright (c) 2025 Jiří Zárevúcky
[16bfcd3]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[696b405]29#include <uchar.h>
30#include <wchar.h>
[16bfcd3]31
[696b405]32#if __STDC_HOSTED__
33#include <fibril.h>
34#endif
35
36wint_t btowc(int c)
37{
38 return (c < 0x80) ? c : WEOF;
39}
[16bfcd3]40
[696b405]41int wctob(wint_t c)
42{
43 return c;
44}
[16bfcd3]45
[696b405]46int mbsinit(const mbstate_t *ps)
47{
[f94a11f]48 return ps == NULL || ps->state == 0;
[696b405]49}
[16bfcd3]50
[696b405]51size_t mbrlen(const char *s, size_t n, mbstate_t *ps)
52{
53#if __STDC_HOSTED__
54 static fibril_local mbstate_t global_state;
55 if (!ps)
56 ps = &global_state;
[16bfcd3]57#endif
58
[696b405]59 return mbrtowc(NULL, s, n, ps);
60}
61
62_Static_assert(sizeof(wchar_t) == sizeof(char16_t) || sizeof(wchar_t) == sizeof(char32_t));
63
64size_t mbrtowc(wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
65{
66#if __STDC_HOSTED__
67 static fibril_local mbstate_t global_state;
68 if (!ps)
69 ps = &global_state;
70#endif
71
72 if (sizeof(wchar_t) == sizeof(char16_t))
73 return mbrtoc16((char16_t *) pwc, s, n, ps);
74 else
75 return mbrtoc32((char32_t *) pwc, s, n, ps);
76}
77
78size_t wcrtomb(char *s, wchar_t wc, mbstate_t * ps)
79{
80#if __STDC_HOSTED__
81 static fibril_local mbstate_t global_state;
82 if (!ps)
83 ps = &global_state;
84#endif
85
86 if (sizeof(wchar_t) == sizeof(char16_t))
87 return c16rtomb(s, (char16_t) wc, ps);
88 else
89 return c32rtomb(s, (char32_t) wc, ps);
90}
Note: See TracBrowser for help on using the repository browser.