Skip to content

Commit f4c308c

Browse files
committed
#70 - Modify method is_space()
Completed.
1 parent 0f1e18f commit f4c308c

File tree

2 files changed

+22
-50
lines changed

2 files changed

+22
-50
lines changed

cpp-strings/cppstrings.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
along with this program. If not, see <https://www.gnu.org/licenses/>.
2020
*/
2121

22+
#include <iostream>
23+
2224
#include "cppstrings.h"
2325
using namespace pcs;
2426

@@ -30,5 +32,7 @@ int main()
3032
CppString s;
3133
CppWString ws;
3234

35+
//std::cout << ws.isspace() << std::endl;
36+
3337
return 0;
3438
}

cpp-strings/cppstrings.h

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -405,49 +405,29 @@ namespace pcs // i.e. "pythonic c++ strings"
405405
}
406406

407407

408-
//--- is_punctuation() --------------------------------
408+
//--- ispunctuation() ---------------------------------
409409
/** \brief Returns true if the string contains only one character and if this character belongs to the ASCII punctuation set. */
410-
inline const bool is_punctuation() const noexcept
410+
inline const bool ispunctuation() const noexcept
411411
{
412412
return this->size() == 1 && pcs::is_punctuation((*this)[0]);
413413
}
414414

415415

416-
//--- is_space() --------------------------------------
417-
/** \brief Returns true if there are only whitespace characters in the string and there is at least one character, or false otherwise.
418-
*
419-
* Notice for version 2.0 of this library: a character is whitespace if
420-
* in the Unicode character database, either its general category is Zs
421-
* (“Separator, space”), or its bidirectional class is one of WS, B, or S.
422-
*/
423-
inline const bool is_space() const noexcept
416+
//--- is_Returns true if there are only whitespace characters in the string and there is at least one character, or false otherwise. */-
417+
inline const bool isspace() const noexcept
424418
{
425419
if (this->size() == 0)
426420
return false;
427-
for (auto& c : *this)
428-
if (!is_space(c))
429-
return false;
430-
return true;
431-
}
432-
433-
/** \brief Returns true if character belongs to the ASCII spaces set. */
434-
static inline const bool is_space(const value_type& ch) noexcept
435-
{
436-
return _ASCII_SPACES.contains(ch);
421+
else
422+
return std::all_of(this->cbegin(), this->cend(), pcs::is_space<CharT>);
437423
}
438424

439425

440426
//--- is_words_sep() ----------------------------------
441427
/** \brief Returns true if there are only whitespace and punctuation characters in the string and there is at least one character, or false otherwise. */
442428
inline const bool is_words_sep() const noexcept
443429
{
444-
return is_space() || is_punctuation();
445-
}
446-
447-
/** \brief Returns true if character belongs to ASCII spaces or punctuation sets. */
448-
static inline const bool is_words_sep(const value_type& ch) noexcept
449-
{
450-
return is_space(ch) || is_punctuation(ch);
430+
return isspace() || ispunctuation();
451431
}
452432

453433

@@ -641,9 +621,6 @@ namespace pcs // i.e. "pythonic c++ strings"
641621

642622

643623
private:
644-
//=== DATA ============================================
645-
static inline constexpr std::vector<value_type> _ASCII_PUNCT_DATA{ '!', ',', '.', ':', ';', '?' };
646-
static inline constexpr std::vector<value_type> _ASCII_SPACES{ ' ', '\t', '\n', 'r', '\f' };
647624

648625
};
649626

@@ -665,36 +642,27 @@ namespace pcs // i.e. "pythonic c++ strings"
665642

666643

667644
//--- is_punctuation ----------------------------------
668-
/** \brief SHOULD NEVER BE USED. Use next specializations instead. */
645+
/** \brief Returns true if character ch is punctuation, or false otherwise. Conforms to the current locale settings. */
669646
template<class CharT>
670647
inline const bool is_punctuation(const CharT ch)
671648
{
672649
static const std::vector<CharT> punct_chars{ '!', ',', '.', ':', ';', '?' };
673650
return std::find(punct_chars.cbegin(), punct_chars.cend(), (ch)) != punct_chars.cend();
674651
}
675652

676-
/** \brief Returns true if character ch is punctuation, or false otherwise. Conforms to the current locale settings. */
677-
/*
653+
654+
//--- is_space() --------------------------------------
655+
/** \brief SHOULD NEVER BE USED. Use next specializations instead. */
656+
template<class CharT>
657+
inline const bool is_space(const CharT ch) { return false; }
658+
659+
/** \brief Returns true if character ch is alphabetic, or false otherwise. Conforms to the current locale settings. */
678660
template<>
679-
inline const bool is_punctuation<char>(const char ch)
680-
{
681-
static const std::vector<char> punct_chars { '!', ',', '.', ':', ';', '?' };
682-
return std::find(punct_chars.cbegin(), punct_chars.cend(), (ch)) != punct_chars.cend();
683-
}
684-
*/
661+
inline const bool is_space<char>(const char ch) { return std::isspace(static_cast<unsigned char>(ch)); }
685662

686-
/** \brief Returns true if character ch is punctuation, or false otherwise. Conforms to the current locale settings. */
687-
/*
663+
/** \brief Returns true if character ch is alphabetic, or false otherwise. Conforms to the current locale settings. */
688664
template<>
689-
inline const bool is_punctuation<wchar_t>(const wchar_t ch)
690-
{
691-
static const std::vector<wchar_t> punct_chars{ '!', ',', '.', ':', ';', '?' };
692-
return std::find(punct_chars.cbegin(), punct_chars.cend(), (ch)) != punct_chars.cend();
693-
}
694-
*/
665+
inline const bool is_space<wchar_t>(const wchar_t ch) { return std::iswspace(ch); }
695666

696-
//--- is_space() --------------------------------------
697-
template<class CharT>
698-
inline const bool is_space(const CharT ch); //!< Returns true if character ch is white space, or false otherwise. */
699667

700668
} // end of namespace pcs // (pythonic c++ strings)

0 commit comments

Comments
 (0)