aboutsummaryrefslogtreecommitdiff
path: root/src/codepoint_iterator.h
diff options
context:
space:
mode:
authorAdrian Kummerländer2013-10-05 12:41:07 +0200
committerAdrian Kummerländer2013-10-05 12:41:07 +0200
commit4d1b9a918f8a189ba1e9887c6a9da04e7392db90 (patch)
treeb6071fa016ba094ad01dc537ce26e540fd61182d /src/codepoint_iterator.h
downloadCodepointIterator-4d1b9a918f8a189ba1e9887c6a9da04e7392db90.tar
CodepointIterator-4d1b9a918f8a189ba1e9887c6a9da04e7392db90.tar.gz
CodepointIterator-4d1b9a918f8a189ba1e9887c6a9da04e7392db90.tar.bz2
CodepointIterator-4d1b9a918f8a189ba1e9887c6a9da04e7392db90.tar.lz
CodepointIterator-4d1b9a918f8a189ba1e9887c6a9da04e7392db90.tar.xz
CodepointIterator-4d1b9a918f8a189ba1e9887c6a9da04e7392db90.tar.zst
CodepointIterator-4d1b9a918f8a189ba1e9887c6a9da04e7392db90.zip
Initial commit
* CodepointIterator is a simple C++ iterator class which iterates through unicode codepoints in a UTF8-encoded string * It is derived from std::iterator and implements the std::bidirectional_iterator_tag * Dereferencing an instance of the class provides the codepoint as char32_t * Tests require Google Test and use UTF8-samples from http://www.columbia.edu/~fdc/utf8/
Diffstat (limited to 'src/codepoint_iterator.h')
-rw-r--r--src/codepoint_iterator.h49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/codepoint_iterator.h b/src/codepoint_iterator.h
new file mode 100644
index 0000000..938f53d
--- /dev/null
+++ b/src/codepoint_iterator.h
@@ -0,0 +1,49 @@
+#ifndef CODEPOINT_ITERATOR_H_
+#define CODEPOINT_ITERATOR_H_
+
+#include <iterator>
+#include <string>
+#include <exception>
+
+namespace UTF8 {
+
+class CodepointIterator : public std::iterator<std::bidirectional_iterator_tag,
+ char32_t,
+ std::string::difference_type,
+ const char32_t*,
+ const char32_t&> {
+ public:
+ CodepointIterator(std::string::const_iterator);
+ CodepointIterator(const CodepointIterator&);
+
+ CodepointIterator& operator=(const CodepointIterator&);
+
+ bool operator==(const CodepointIterator&) const;
+ bool operator==(const std::string::const_iterator&) const;
+
+ bool operator!=(const CodepointIterator&) const;
+ bool operator!=(const std::string::const_iterator&) const;
+
+ char32_t operator*();
+
+ CodepointIterator& operator++();
+ CodepointIterator& operator--();
+
+ CodepointIterator operator++(int);
+ CodepointIterator operator--(int);
+
+ private:
+ std::string::const_iterator iterator_;
+ bool dereferenced_;
+ char32_t codepoint_;
+};
+
+class codepoint_invalid: public std::exception {
+ virtual const char* what() const throw() {
+ return "codepoint_invalid";
+ }
+};
+
+}
+
+#endif // CODEPOINT_ITERATOR_H_