Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
bec6fa4a
Commit
bec6fa4a
authored
Apr 13, 2016
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lib/icu/Converter: throw exception on error
parent
ac7ce735
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
23 additions
and
20 deletions
+23
-20
Charset.cxx
src/fs/Charset.cxx
+5
-4
Converter.cxx
src/lib/icu/Converter.cxx
+11
-12
Converter.hxx
src/lib/icu/Converter.hxx
+2
-2
TestIcu.cxx
test/TestIcu.cxx
+5
-2
No files found.
src/fs/Charset.cxx
View file @
bec6fa4a
...
...
@@ -114,11 +114,12 @@ PathToUTF8(PathTraitsFS::const_pointer_type path_fs)
return
FixSeparators
(
path_fs
);
#ifdef HAVE_FS_CHARSET
const
auto
buffer
=
fs_converter
->
ToUTF8
(
path_fs
);
if
(
buffer
.
IsNull
())
try
{
const
auto
buffer
=
fs_converter
->
ToUTF8
(
path_fs
);
return
FixSeparators
(
PathTraitsUTF8
::
string
(
buffer
.
c_str
()));
}
catch
(
const
std
::
runtime_error
&
)
{
return
PathTraitsUTF8
::
string
();
return
FixSeparators
(
PathTraitsUTF8
::
string
(
buffer
.
c_str
()));
}
#endif
#endif
}
...
...
src/lib/icu/Converter.cxx
View file @
bec6fa4a
...
...
@@ -90,8 +90,11 @@ DoConvert(iconv_t conv, const char *src)
size_t
n
=
iconv
(
conv
,
&
in
,
&
in_left
,
&
out
,
&
out_left
);
if
(
n
==
static_cast
<
size_t
>
(
-
1
)
||
in_left
>
0
)
return
nullptr
;
if
(
n
==
static_cast
<
size_t
>
(
-
1
))
throw
MakeErrno
(
"Charset conversion failed"
);
if
(
in_left
>
0
)
throw
std
::
runtime_error
(
"Charset conversion failed"
);
return
AllocatedString
<>::
Duplicate
(
buffer
,
sizeof
(
buffer
)
-
out_left
);
}
...
...
@@ -100,7 +103,7 @@ DoConvert(iconv_t conv, const char *src)
AllocatedString
<
char
>
IcuConverter
::
ToUTF8
(
const
char
*
s
)
const
try
{
{
#ifdef HAVE_ICU
const
ScopeLock
protect
(
mutex
);
...
...
@@ -116,26 +119,23 @@ try {
&
source
,
source
+
strlen
(
source
),
nullptr
,
true
,
&
code
);
if
(
code
!=
U_ZERO_ERROR
)
return
nullptr
;
throw
std
::
runtime_error
(
FormatString
(
"Failed to convert to Unicode: %s"
,
u_errorName
(
code
)).
c_str
());
const
size_t
target_length
=
target
-
buffer
;
return
UCharToUTF8
({
buffer
,
target_length
});
#elif defined(HAVE_ICONV)
return
DoConvert
(
to_utf8
,
s
);
#endif
}
catch
(
const
std
::
runtime_error
&
)
{
return
nullptr
;
}
AllocatedString
<
char
>
IcuConverter
::
FromUTF8
(
const
char
*
s
)
const
try
{
{
#ifdef HAVE_ICU
const
ScopeLock
protect
(
mutex
);
const
auto
u
=
UCharFromUTF8
(
s
);
if
(
u
.
IsNull
())
return
nullptr
;
ucnv_resetFromUnicode
(
converter
);
...
...
@@ -149,15 +149,14 @@ try {
nullptr
,
true
,
&
code
);
if
(
code
!=
U_ZERO_ERROR
)
return
nullptr
;
throw
std
::
runtime_error
(
FormatString
(
"Failed to convert from Unicode: %s"
,
u_errorName
(
code
)).
c_str
());
return
AllocatedString
<>::
Duplicate
(
buffer
,
target
);
#elif defined(HAVE_ICONV)
return
DoConvert
(
from_utf8
,
s
);
#endif
}
catch
(
const
std
::
runtime_error
&
)
{
return
nullptr
;
}
#endif
src/lib/icu/Converter.hxx
View file @
bec6fa4a
...
...
@@ -79,7 +79,7 @@ public:
/**
* Convert the string to UTF-8.
*
*
Returns AllocatedString::Null()
on error.
*
Throws std::runtime_error
on error.
*/
gcc_pure
gcc_nonnull_all
AllocatedString
<
char
>
ToUTF8
(
const
char
*
s
)
const
;
...
...
@@ -87,7 +87,7 @@ public:
/**
* Convert the string from UTF-8.
*
*
Returns AllocatedString::Null()
on error.
*
Throws std::runtime_error
on error.
*/
gcc_pure
gcc_nonnull_all
AllocatedString
<
char
>
FromUTF8
(
const
char
*
s
)
const
;
...
...
test/TestIcu.cxx
View file @
bec6fa4a
...
...
@@ -54,8 +54,11 @@ public:
CPPUNIT_ASSERT
(
converter
!=
nullptr
);
for
(
const
auto
i
:
invalid_utf8
)
{
auto
f
=
converter
->
FromUTF8
(
i
);
CPPUNIT_ASSERT_EQUAL
(
true
,
f
.
IsNull
());
try
{
auto
f
=
converter
->
FromUTF8
(
i
);
CPPUNIT_FAIL
(
"Exception expected"
);
}
catch
(
const
std
::
runtime_error
&
)
{
}
}
for
(
const
auto
i
:
latin1_tests
)
{
...
...
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