Skip to content

Commit

Permalink
Reusable iteration of glyphs in a string.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtv committed Jan 27, 2019
1 parent 6903d5e commit 1ac9662
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
24 changes: 24 additions & 0 deletions include/pqxx/internal/encodings.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ std::string::size_type find_with_encoding(
std::string::size_type start = 0
);


/// Iterate over the glyphs in a buffer.
/** Scans the glyphs in the buffer, and for each, passes its begin and its
* one-past-end pointers to @c callback.
*/
template<typename CALLBACK> inline void for_glyphs(
encoding_group enc,
CALLBACK callback,
const char buffer[],
std::string::size_type buffer_len,
std::string::size_type start = 0
)
{
const auto scan = get_glyph_scanner(enc);
for (
std::string::size_type here = start, next;
here < buffer_len;
here = next
)
{
next = scan(buffer, buffer_len, here);
callback(buffer + here, buffer + next);
}
}
} // namespace pqxx::internal
} // namespace pqxx

Expand Down
2 changes: 1 addition & 1 deletion src/array.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** Handling of SQL arrays.
*
* Copyright (c) 2019, Jeroen T. Vermeulen.
* Copyright (c) 2018-2019, Jeroen T. Vermeulen.
*
* See COPYING for copyright license. If you did not receive a file called
* COPYING with this source code, please notify the distributor of this mistake,
Expand Down
23 changes: 11 additions & 12 deletions src/sql_cursor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,18 @@ std::string::size_type find_query_end(
// Complex encoding. We only know how to iterate forwards, so start from
// the beginning.
end = 0;
const auto scan = pqxx::internal::get_glyph_scanner(enc);
for (
auto here = 0ul, next = 0ul;
here < size;
here = next
)
{
next = scan(text, size, here);
if (next - here > 1 or not useless_trail(text[here]))
end = next;
}

pqxx::internal::for_glyphs(
enc,
[text, &end](const char *gbegin, const char *gend)
{
if (gend - gbegin > 1 or not useless_trail(*gbegin))
end = gend - text;
},
text, size);
}
return end;

return end;
}
} // namespace

Expand Down

0 comments on commit 1ac9662

Please sign in to comment.