diff options
author | Florent Le Coz <louiz@louiz.org> | 2013-11-09 06:01:47 +0100 |
---|---|---|
committer | Florent Le Coz <louiz@louiz.org> | 2013-11-09 06:01:47 +0100 |
commit | ccebe901d7d76dfddc082d994efa54ef2aefee57 (patch) | |
tree | 97a542cce7c3c3185553859f679dc074f8f0286f /src/utils/encoding.hpp | |
parent | a418b6ed5d70f0e61e71bb1adce2a693ade89e30 (diff) | |
download | biboumi-ccebe901d7d76dfddc082d994efa54ef2aefee57.tar.gz biboumi-ccebe901d7d76dfddc082d994efa54ef2aefee57.tar.bz2 biboumi-ccebe901d7d76dfddc082d994efa54ef2aefee57.tar.xz biboumi-ccebe901d7d76dfddc082d994efa54ef2aefee57.zip |
Check UTF-8 encoding, and convert strings to UTF-8
Handle conversion errors properly by inserting � instead. Add a binary
header to provide portable way to write binary literals (I like them) Also
add a test file. ref #2404
Diffstat (limited to 'src/utils/encoding.hpp')
-rw-r--r-- | src/utils/encoding.hpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/utils/encoding.hpp b/src/utils/encoding.hpp new file mode 100644 index 0000000..362f1df --- /dev/null +++ b/src/utils/encoding.hpp @@ -0,0 +1,21 @@ +#ifndef ENCODING_INCLUDED +# define ENCODING_INCLUDED + +#include <string> + +namespace utils +{ + /** + * Returns true if the given null-terminated string is valid utf-8. + * + * Based on http://en.wikipedia.org/wiki/UTF-8#Description + */ + bool is_valid_utf8(const char* s); + /** + * Convert the given string (encoded is "encoding") into valid utf-8. + * If some decoding fails, insert an utf-8 placeholder character instead. + */ + std::string convert_to_utf8(const std::string& str, const char* encoding); +} + +#endif // ENCODING_INCLUDED |