Make ui::edit::numeric validators accept max value length

This commit is contained in:
Nikita Lisitsa 2022-05-30 22:13:28 +03:00
parent 0c3f4c3134
commit 65d95d9b5a
2 changed files with 8 additions and 8 deletions

View file

@ -46,8 +46,8 @@ namespace psemek::ui
using validator_type = std::function<bool(std::u32string_view)>;
virtual bool set_validator(validator_type validator);
static validator_type numeric_nonnegative();
static validator_type numeric();
static validator_type numeric_nonnegative(int max_length);
static validator_type numeric(int max_length);
using callback_type = std::function<void(std::u32string_view)>;
virtual void on_text_entered(callback_type callback, bool signal = true);

View file

@ -63,22 +63,22 @@ namespace psemek::ui
return true;
}
edit::validator_type edit::numeric()
edit::validator_type edit::numeric(int max_length)
{
return [](std::u32string_view const str) -> bool {
return [max_length](std::u32string_view const str) -> bool {
auto begin = str.begin();
auto end = str.end();
if (begin != end && *begin == '-')
++begin;
return std::all_of(begin, end, [](char32_t c){ return '0' <= c && c <= '9'; });
return std::all_of(begin, end, [](char32_t c){ return '0' <= c && c <= '9'; }) && (end - begin) <= max_length;
};
}
edit::validator_type edit::numeric_nonnegative()
edit::validator_type edit::numeric_nonnegative(int max_length)
{
return [](std::u32string_view const str) -> bool {
return std::all_of(str.begin(), str.end(), [](char32_t c){ return '0' <= c && c <= '9'; });
return [max_length](std::u32string_view const str) -> bool {
return std::all_of(str.begin(), str.end(), [](char32_t c){ return '0' <= c && c <= '9'; }) && str.length() <= max_length;
};
}