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
666f700a
Commit
666f700a
authored
Oct 10, 2014
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
TagString: implement fix_utf8() without GLib
parent
b70bf938
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
22 deletions
+34
-22
TagString.cxx
src/tag/TagString.cxx
+34
-22
No files found.
src/tag/TagString.cxx
View file @
666f700a
...
...
@@ -21,30 +21,53 @@
#include "TagString.hxx"
#include "util/Alloc.hxx"
#include "util/WritableBuffer.hxx"
#ifdef HAVE_GLIB
#include <glib.h>
#endif
#include "util/UTF8.hxx"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_GLIB
gcc_pure
static
const
char
*
FindInvalidUTF8
(
const
char
*
p
,
const
char
*
const
end
)
{
while
(
p
<
end
)
{
const
size_t
s
=
SequenceLengthUTF8
(
*
p
);
if
(
p
+
s
>
end
)
/* partial sequence at end of string */
return
p
;
/* now call the other SequenceLengthUTF8() overload
which also validates the continuations */
const
size_t
t
=
SequenceLengthUTF8
(
p
);
assert
(
s
==
t
);
if
(
t
==
0
)
return
p
;
p
+=
s
;
}
return
nullptr
;
}
/**
* Replace invalid sequences with the question mark.
*/
static
WritableBuffer
<
char
>
patch_utf8
(
const
char
*
src
,
size_t
length
,
const
gchar
*
en
d
)
patch_utf8
(
const
char
*
src
,
size_t
length
,
const
char
*
_invali
d
)
{
/* duplicate the string, and replace invalid bytes in that
buffer */
char
*
dest
=
(
char
*
)
xmemdup
(
src
,
length
);
char
*
const
end
=
dest
+
length
;
char
*
invalid
=
dest
+
(
_invalid
-
src
);
do
{
dest
[
end
-
src
]
=
'?'
;
}
while
(
!
g_utf8_validate
(
end
+
1
,
(
src
+
length
)
-
(
end
+
1
),
&
end
));
*
invalid
=
'?'
;
const
char
*
__invalid
=
FindInvalidUTF8
(
invalid
+
1
,
end
);
invalid
=
const_cast
<
char
*>
(
__invalid
);
}
while
(
invalid
!=
nullptr
);
return
{
dest
,
length
};
}
...
...
@@ -52,20 +75,15 @@ patch_utf8(const char *src, size_t length, const gchar *end)
static
WritableBuffer
<
char
>
fix_utf8
(
const
char
*
str
,
size_t
length
)
{
const
gchar
*
end
;
assert
(
str
!=
nullptr
);
/* check if the string is already valid UTF-8 */
if
(
g_utf8_validate
(
str
,
length
,
&
end
))
const
char
*
invalid
=
FindInvalidUTF8
(
str
,
str
+
length
);
if
(
invalid
==
nullptr
)
return
nullptr
;
/* no, broken - patch invalid sequences */
return
patch_utf8
(
str
,
length
,
en
d
);
return
patch_utf8
(
str
,
length
,
invali
d
);
}
#endif
static
bool
char_is_non_printable
(
unsigned
char
ch
)
{
...
...
@@ -105,23 +123,17 @@ clear_non_printable(const char *p, size_t length)
WritableBuffer
<
char
>
FixTagString
(
const
char
*
p
,
size_t
length
)
{
#ifdef HAVE_GLIB
// TODO: implement without GLib
auto
utf8
=
fix_utf8
(
p
,
length
);
if
(
!
utf8
.
IsNull
())
{
p
=
utf8
.
data
;
length
=
utf8
.
size
;
}
#endif
WritableBuffer
<
char
>
cleared
=
clear_non_printable
(
p
,
length
);
#ifdef HAVE_GLIB
if
(
cleared
.
IsNull
())
cleared
=
utf8
;
else
free
(
utf8
.
data
);
#endif
return
cleared
;
}
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