Skip to content

Commit 66d1acd

Browse files
committed
#24 - Implement method CppStringT::isdecimal()
Completed.
1 parent 4903ec2 commit 66d1acd

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

cpp-strings/cppstrings.h

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ namespace pcs // i.e. "pythonic c++ strings"
5454
template<class CharT>
5555
inline const bool is_ascii(const CharT ch) noexcept; //!< Returns true if character ch gets ASCII code, or false otherwise.
5656

57+
template<class CharT>
58+
inline const bool is_decimal(const CharT ch) noexcept; //!< Returns true if character is a decimal digit, or false otherwise.
59+
5760
template<class CharT>
5861
inline const bool is_punctuation(const CharT ch) noexcept; //!< Returns true if character ch is punctuation, or false otherwise.
5962

@@ -380,28 +383,31 @@ namespace pcs // i.e. "pythonic c++ strings"
380383
/** \brief Returns true if all characters in the string are alphabetic and there is at least one character, or false otherwise. */
381384
inline const bool isalpha() const noexcept
382385
{
383-
if (this->size() == 0)
384-
return false;
385-
else
386-
return std::all_of(this->cbegin(), this->cend(), [](const value_type ch) { return pcs::is_alpha<CharT>(ch); });
386+
return this->size() > 0 && std::all_of(this->cbegin(), this->cend(), [](const value_type ch) { return pcs::is_alpha<CharT>(ch); });
387387
}
388388

389389

390390
//--- isascii() ---------------------------------------
391391
/** \brief Returns true if the string is empty or all characters in the string are ASCII, or false otherwise. */
392-
#if defined(isascii) // may be already defined in header file <ctype>
392+
#if defined(isascii) // may be already defined in header file <ctype.h>
393393
#undef isascii
394394
#endif
395395
inline const bool isascii() const noexcept
396396
{
397-
return this->size() == 0 || std::all_of(this->cbegin(), this->cend(), pcs::is_ascii<CharT>);
397+
return this->size() == 0 || std::all_of(this->cbegin(), this->cend(), pcs::is_ascii<CharT>);
398398
}
399399

400400

401401
//--- isdecimal() -------------------------------------
402+
/** \brief Returns true if all characters in the string are decimal characters and there is at least one character, or false otherwise.
403+
*
404+
* Decimal characters are those that can be used to form numbers in
405+
* base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal
406+
* character is a character in the Unicode General Category “Nd”.
407+
*/
402408
inline const bool isdecimal() const noexcept
403409
{
404-
return false;
410+
return this->size() > 0 && std::all_of(this->cbegin(), this->cend(), pcs::is_decimal<CharT>);
405411
}
406412

407413

@@ -666,15 +672,45 @@ namespace pcs // i.e. "pythonic c++ strings"
666672
{ return CharT(0x00) <= ch && ch <= CharT(0x7f); }
667673

668674

669-
//--- is_punctuation() ------------------------------------
670-
/** \brief Returns true if character ch is punctuation, or false otherwise. Conforms to the current locale settings. */
675+
//--- is_decimal() ----------------------------------------
676+
/** \brief SHOULD NEVER BE USED. Use next specializations instead. */
671677
template<class CharT>
672-
inline const bool is_punctuation(const CharT ch) noexcept
678+
inline const bool is_decimal(const CharT ch) noexcept
679+
{
680+
return false;
681+
}
682+
683+
/** \brief Returns true if character is a decimal digit, or false otherwise. */
684+
template<>
685+
inline const bool is_decimal<char>(const char ch) noexcept
673686
{
674-
static const std::vector<CharT> punct_chars{ '!', ',', '.', ':', ';', '?' };
675-
return std::find(punct_chars.cbegin(), punct_chars.cend(), (ch)) != punct_chars.cend();
687+
return std::isdigit(static_cast<unsigned char>(ch));
676688
}
677689

690+
/** \brief Returns true if character is a decimal digit, or false otherwise. */
691+
template<>
692+
inline const bool is_decimal<wchar_t>(const wchar_t ch) noexcept
693+
{
694+
return std::isdigit(ch);
695+
}
696+
697+
698+
//--- is_punctuation() ------------------------------------
699+
/** \brief SHOULD NEVER BE USED. Use next specializations instead. */
700+
template<class CharT>
701+
inline const bool is_punctuation(const CharT ch) noexcept
702+
{ return false; }
703+
704+
/** \brief Returns true if character ch is punctuation, or false otherwise. Conforms to the current locale settings. */
705+
template<>
706+
inline const bool is_punctuation<char>(const char ch) noexcept
707+
{ return std::ispunct(static_cast<unsigned char>(ch)); }
708+
709+
/** \brief Returns true if character ch is punctuation, or false otherwise. Conforms to the current locale settings. */
710+
template<>
711+
inline const bool is_punctuation<wchar_t>(const wchar_t ch) noexcept
712+
{ return std::iswpunct(ch); }
713+
678714

679715
//--- is_space() ------------------------------------------
680716
/** \brief SHOULD NEVER BE USED. Use next specializations instead. */

0 commit comments

Comments
 (0)