From 65d95d9b5a8a5d285043f30745cd524fa0716230 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Mon, 30 May 2022 22:13:28 +0300 Subject: [PATCH] Make ui::edit::numeric validators accept max value length --- libs/ui/include/psemek/ui/edit.hpp | 4 ++-- libs/ui/source/edit.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libs/ui/include/psemek/ui/edit.hpp b/libs/ui/include/psemek/ui/edit.hpp index ad54c6f4..4374dd37 100644 --- a/libs/ui/include/psemek/ui/edit.hpp +++ b/libs/ui/include/psemek/ui/edit.hpp @@ -46,8 +46,8 @@ namespace psemek::ui using validator_type = std::function; 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; virtual void on_text_entered(callback_type callback, bool signal = true); diff --git a/libs/ui/source/edit.cpp b/libs/ui/source/edit.cpp index fb7188f0..9ce87857 100644 --- a/libs/ui/source/edit.cpp +++ b/libs/ui/source/edit.cpp @@ -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; }; }