Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
da9aeab8
Commit
da9aeab8
authored
Apr 30, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add UTF8ToUTF16 and UTF16ToUTF8.
parent
859a4975
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
68 additions
and
1 deletion
+68
-1
format.cc
format.cc
+36
-0
format.h
format.h
+32
-1
No files found.
format.cc
View file @
da9aeab8
...
...
@@ -167,6 +167,42 @@ void fmt::internal::ReportUnknownType(char code, const char *type) {
<<
static_cast
<
unsigned
>
(
code
)
<<
type
));
}
#ifdef _WIN32
fmt
::
internal
::
UTF8ToUTF16
::
UTF8ToUTF16
(
fmt
::
StringRef
s
)
{
int
length
=
MultiByteToWideChar
(
CP_UTF8
,
MB_ERR_INVALID_CHARS
,
s
.
c_str
(),
-
1
,
0
,
0
);
static
const
char
ERROR
[]
=
"cannot convert string from UTF-8 to UTF-16"
;
if
(
length
==
0
)
ThrowSystemError
(
GetLastError
(),
ERROR
);
buffer_
.
resize
(
length
);
length
=
MultiByteToWideChar
(
CP_UTF8
,
MB_ERR_INVALID_CHARS
,
s
.
c_str
(),
-
1
,
&
buffer_
[
0
],
length
);
if
(
length
==
0
)
ThrowSystemError
(
GetLastError
(),
ERROR
);
}
fmt
::
internal
::
UTF16ToUTF8
::
UTF16ToUTF8
(
fmt
::
WStringRef
s
)
{
if
(
int
error_code
=
Convert
(
s
))
{
ThrowSystemError
(
GetLastError
(),
"cannot convert string from UTF-16 to UTF-8"
);
}
}
int
fmt
::
internal
::
UTF16ToUTF8
::
Convert
(
fmt
::
WStringRef
s
)
{
int
length
=
WideCharToMultiByte
(
CP_UTF8
,
0
,
s
.
c_str
(),
-
1
,
0
,
0
,
0
,
0
);
if
(
length
==
0
)
return
GetLastError
();
buffer_
.
resize
(
length
);
length
=
WideCharToMultiByte
(
CP_UTF8
,
0
,
s
.
c_str
(),
-
1
,
&
buffer_
[
0
],
length
,
0
,
0
);
if
(
length
==
0
)
return
GetLastError
();
return
0
;
}
#endif
void
fmt
::
internal
::
FormatSystemErrorMessage
(
fmt
::
Writer
&
out
,
int
error_code
,
fmt
::
StringRef
message
)
{
#ifndef _WIN32
...
...
format.h
View file @
da9aeab8
...
...
@@ -458,6 +458,37 @@ template <typename Char, typename T>
void
FormatCustomArg
(
BasicWriter
<
Char
>
&
w
,
const
void
*
arg
,
const
FormatSpec
&
spec
);
#ifdef _WIN32
// A converter from UTF-8 to UTF-16.
// It is only provided for Windows since other systems use UTF-8.
class
UTF8ToUTF16
{
private:
Array
<
wchar_t
,
INLINE_BUFFER_SIZE
>
buffer_
;
public:
explicit
UTF8ToUTF16
(
StringRef
s
);
operator
const
wchar_t
*
()
const
{
return
&
buffer_
[
0
];
}
size_t
size
()
const
{
return
buffer_
.
size
()
-
1
;
}
};
// A converter from UTF-16 to UTF-8.
// It is only provided for Windows since other systems use UTF-8.
class
UTF16ToUTF8
{
private:
Array
<
char
,
INLINE_BUFFER_SIZE
>
buffer_
;
public:
UTF16ToUTF8
()
{}
explicit
UTF16ToUTF8
(
WStringRef
s
);
operator
const
char
*
()
const
{
return
&
buffer_
[
0
];
}
size_t
size
()
const
{
return
buffer_
.
size
()
-
1
;
}
// Performs conversion returning a system error code instead of
// throwing exception on error.
int
Convert
(
WStringRef
s
);
};
#endif
// Formats a system error message writing the output to out.
void
FormatSystemErrorMessage
(
Writer
&
out
,
int
error_code
,
StringRef
message
);
...
...
@@ -1507,7 +1538,7 @@ class SystemErrorSink {
/** Throws SystemError with a code and a formatted message. */
inline
Formatter
<
SystemErrorSink
>
ThrowSystemError
(
int
error_code
,
StringRef
format
)
{
int
error_code
,
StringRef
format
=
0
)
{
Formatter
<
SystemErrorSink
>
f
(
format
,
SystemErrorSink
(
error_code
));
return
f
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment