2 * Copyright 2018 Max Kellermann <max.kellermann@gmail.com>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * 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
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "LocaleString.hxx"
37 IsIncompleteCharMB(const char *s, size_t n)
39 auto mb = std::mbstate_t();
40 const std::size_t length = std::mbrlen(s, n, &mb);
41 return length == std::size_t(-2);
45 CharSizeMB(const char *s, size_t n)
47 auto mb = std::mbstate_t();
48 const std::size_t length = std::mbrlen(s, n, &mb);
49 if (length == std::size_t(-2))
52 if (length == std::size_t(-1))
59 PrevCharMB(const char *start, const char *reference)
61 const char *p = reference;
66 auto mb = std::mbstate_t();
67 const std::size_t length = std::mbrlen(p, reference - p, &mb);
68 if (length != std::size_t(-1))
76 StringWidthMB(const char *s, size_t length)
78 const char *const end = s + length;
79 auto state = std::mbstate_t();
84 std::size_t n = std::mbrtowc(&w, s, end - s, &state);
85 if (n == std::size_t(-2))
88 if (n == std::size_t(-1) || n == 0) {
102 StringWidthMB(const char *s)
104 return StringWidthMB(s, strlen(s));