summaryrefslogtreecommitdiff
path: root/src/utils/binary.hpp
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2013-11-09 06:01:47 +0100
committerFlorent Le Coz <louiz@louiz.org>2013-11-09 06:01:47 +0100
commitccebe901d7d76dfddc082d994efa54ef2aefee57 (patch)
tree97a542cce7c3c3185553859f679dc074f8f0286f /src/utils/binary.hpp
parenta418b6ed5d70f0e61e71bb1adce2a693ade89e30 (diff)
downloadbiboumi-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/binary.hpp')
-rw-r--r--src/utils/binary.hpp16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/utils/binary.hpp b/src/utils/binary.hpp
new file mode 100644
index 0000000..10807bc
--- /dev/null
+++ b/src/utils/binary.hpp
@@ -0,0 +1,16 @@
+#ifndef BINARY_INCLUDED
+# define BINARY_INCLUDED
+
+template<char FIRST, char... REST> struct binary
+{
+ static_assert(FIRST == '0' || FIRST == '1', "invalid binary digit" );
+ enum { value = ((FIRST - '0') << sizeof...(REST)) + binary<REST...>::value };
+};
+
+template<> struct binary<'0'> { enum { value = 0 }; };
+template<> struct binary<'1'> { enum { value = 1 }; };
+
+template<char... LITERAL> inline
+constexpr unsigned int operator "" _b() { return binary<LITERAL...>::value; }
+
+#endif // BINARY_INCLUDED