Files
textmate/Frameworks/text/src/utf16.h
Allan Odgaard 7d0100fa2b utf16::distance: End iterator may point into multi-byte character
This function is (indirectly) used by a lot of code, and not all of it provide with valid indexes, though it seems like an issue that can be fixed locally, hence why I have decided to allow it (coupled with this being the main reason for crashes).

It is however still not allowed when building in debug mode (rationale being that running it in debug mode and getting an assertion failure should provide enough info to fix the issue).
2014-04-05 14:13:39 +07:00

41 lines
965 B
C++

#ifndef UTF16_H_VKRVH0HR
#define UTF16_H_VKRVH0HR
#include "utf8.h"
#include <oak/oak.h>
namespace utf16
{
template <typename _Iter>
_Iter advance (_Iter const& first, size_t distance)
{
utf8::iterator_t<char const*> it(first);
for(; distance; ++it)
distance -= (*it > 0xFFFF) ? 2 : 1;
return &it;
}
template <typename _Iter>
_Iter advance (_Iter const& first, size_t distance, _Iter const& last)
{
ASSERT(last == utf8::find_safe_end(first, last));
utf8::iterator_t<char const*> it(first);
for(; distance && &it != last; ++it)
distance -= (*it > 0xFFFF) ? 2 : 1;
return &it;
}
template <typename _Iter>
size_t distance (_Iter const& first, _Iter const& last)
{
ASSERT(last == utf8::find_safe_end(first, last));
size_t res = 0;
foreach(it, utf8::make(first), utf8::make(utf8::find_safe_end(first, last)))
res += (*it > 0xFFFF) ? 2 : 1;
return res;
}
} /* utf16 */
#endif /* end of include guard: UTF16_H_VKRVH0HR */