Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: src/common/tuklib_mbstr_width.c

Issue 7109015: Update XZ Utils to 5.0.3 (in deps) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/xz/
Patch Set: Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/common/tuklib_mbstr_fw.c ('k') | src/liblzma/Makefile.am » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file tuklib_mstr_width.c
4 /// \brief Calculate width of a multibyte string
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
10 //
11 ///////////////////////////////////////////////////////////////////////////////
12
13 #include "tuklib_mbstr.h"
14
15 #if defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH)
16 # include <wchar.h>
17 #endif
18
19
20 extern size_t
21 tuklib_mbstr_width(const char *str, size_t *bytes)
22 {
23 const size_t len = strlen(str);
24 if (bytes != NULL)
25 *bytes = len;
26
27 #if !(defined(HAVE_MBRTOWC) && defined(HAVE_WCWIDTH))
28 // In single-byte mode, the width of the string is the same
29 // as its length.
30 return len;
31
32 #else
33 mbstate_t state;
34 memset(&state, 0, sizeof(state));
35
36 size_t width = 0;
37 size_t i = 0;
38
39 // Convert one multibyte character at a time to wchar_t
40 // and get its width using wcwidth().
41 while (i < len) {
42 wchar_t wc;
43 const size_t ret = mbrtowc(&wc, str + i, len - i, &state);
44 if (ret < 1 || ret > len)
45 return (size_t)-1;
46
47 i += ret;
48
49 const int wc_width = wcwidth(wc);
50 if (wc_width < 0)
51 return (size_t)-1;
52
53 width += wc_width;
54 }
55
56 // Require that the string ends in the initial shift state.
57 // This way the caller can be combine the string with other
58 // strings without needing to worry about the shift states.
59 if (!mbsinit(&state))
60 return (size_t)-1;
61
62 return width;
63 #endif
64 }
OLDNEW
« no previous file with comments | « src/common/tuklib_mbstr_fw.c ('k') | src/liblzma/Makefile.am » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698