Avoid unneeded iterator copies in utf8.hh

This commit is contained in:
Maxime Coste 2015-09-23 19:48:15 +01:00
parent 27571a7716
commit ceafa5459a

View File

@ -15,7 +15,7 @@ namespace utf8
// returns an iterator to next character first byte // returns an iterator to next character first byte
template<typename Iterator> template<typename Iterator>
Iterator next(Iterator it, Iterator end) Iterator next(Iterator it, const Iterator& end)
{ {
if (it != end and *it++ & 0x80) if (it != end and *it++ & 0x80)
while (it != end and (*(it) & 0xC0) == 0x80) while (it != end and (*(it) & 0xC0) == 0x80)
@ -26,7 +26,7 @@ Iterator next(Iterator it, Iterator end)
// returns it's parameter if it points to a character first byte, // returns it's parameter if it points to a character first byte,
// or else returns next character first byte // or else returns next character first byte
template<typename Iterator> template<typename Iterator>
Iterator finish(Iterator it, Iterator end) Iterator finish(Iterator it, const Iterator& end)
{ {
while (it != end and (*(it) & 0xC0) == 0x80) while (it != end and (*(it) & 0xC0) == 0x80)
++it; ++it;
@ -35,7 +35,7 @@ Iterator finish(Iterator it, Iterator end)
// returns an iterator to the previous character first byte // returns an iterator to the previous character first byte
template<typename Iterator> template<typename Iterator>
Iterator previous(Iterator it, Iterator begin) Iterator previous(Iterator it, const Iterator& begin)
{ {
while (it != begin and (*(--it) & 0xC0) == 0x80) while (it != begin and (*(--it) & 0xC0) == 0x80)
; ;
@ -46,7 +46,7 @@ Iterator previous(Iterator it, Iterator begin)
// dth character after (or before if d < 0) the character // dth character after (or before if d < 0) the character
// pointed by it // pointed by it
template<typename Iterator> template<typename Iterator>
Iterator advance(Iterator it, Iterator end, CharCount d) Iterator advance(Iterator it, const Iterator& end, CharCount d)
{ {
if (d < 0) if (d < 0)
{ {
@ -63,7 +63,7 @@ Iterator advance(Iterator it, Iterator end, CharCount d)
// returns the character count between begin and end // returns the character count between begin and end
template<typename Iterator> template<typename Iterator>
CharCount distance(Iterator begin, Iterator end) CharCount distance(Iterator begin, const Iterator& end)
{ {
CharCount dist = 0; CharCount dist = 0;
while (begin != end) while (begin != end)
@ -83,7 +83,7 @@ inline bool is_character_start(char c)
// returns an iterator to the first byte of the character it is into // returns an iterator to the first byte of the character it is into
template<typename Iterator> template<typename Iterator>
Iterator character_start(Iterator it, Iterator begin) Iterator character_start(Iterator it, const Iterator& begin)
{ {
while (it != begin and not is_character_start(*it)) while (it != begin and not is_character_start(*it))
--it; --it;
@ -109,7 +109,7 @@ struct Pass
// is pointed by it // is pointed by it
template<typename InvalidPolicy = utf8::InvalidPolicy::Pass, template<typename InvalidPolicy = utf8::InvalidPolicy::Pass,
typename Iterator> typename Iterator>
Codepoint codepoint(Iterator it, Iterator end) Codepoint codepoint(Iterator it, const Iterator& end)
{ {
if (it == end) if (it == end)
return InvalidPolicy{}(-1); return InvalidPolicy{}(-1);