From 0b528c722580182588e35f6c40901c79ce8d56b8 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sun, 11 Mar 2012 16:21:52 +0000 Subject: [PATCH] added: to misc: void OnlyDigit(StringType & s, bool allow_comma = true) removes all non-digit characters from a string git-svn-id: svn://ttmath.org/publicrep/winix/trunk@818 e52654a7-88a9-db11-a3e9-0013d4bc506e --- core/misc.cpp | 32 ++++++++++++++++---------------- core/misc.h | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/core/misc.cpp b/core/misc.cpp index ee7534f..30646f7 100755 --- a/core/misc.cpp +++ b/core/misc.cpp @@ -622,11 +622,11 @@ std::wstring::size_type i; } - -bool IsEmailCorrectChar(wchar_t c) -{ -bool correct = false; - + +bool IsEmailCorrectChar(wchar_t c) +{ +bool correct = false; + const wchar_t * allowed_chars = L"@.!#$%&'*+-/=?^_`{|}~"; if( (c >= 'A' && c<='Z') || @@ -646,12 +646,12 @@ bool correct = false; } } } - -return correct; -} - - - + +return correct; +} + + + bool ValidateEmail(const wchar_t * email) { int at = 0; // how many '@' @@ -674,13 +674,13 @@ bool ValidateEmail(const wchar_t * email) return true; } - - + + bool ValidateEmail(const std::wstring & email) { - return ValidateEmail(email.c_str()); -} - + return ValidateEmail(email.c_str()); +} + diff --git a/core/misc.h b/core/misc.h index 1c7a570..7f62fae 100755 --- a/core/misc.h +++ b/core/misc.h @@ -258,6 +258,42 @@ void MaxSize(StringType & str, size_t max_size) +/* + this method removing all characters from given string + only digits are allowed and if allow_comma then one comma (or dot) + character is allowed +*/ +template +void OnlyDigit(StringType & s, bool allow_comma = true) +{ +typename StringType::size_type i; +bool was_comma = false; + + if( s.empty() ) + { + s = '0'; + return; + } + + for(i=0 ; i='0' && s[i]<='9') || + (allow_comma && !was_comma && (s[i]=='.' || s[i]==',')) ) + { + if( s[i]=='.' || s[i]==',' ) + was_comma = true; + + i += 1; + } + else + { + s.erase(i, 1); + } + } +} + + + wchar_t ToSmall(wchar_t c); void ToSmall(std::wstring & s);