Updated:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
if (m_parse_success == false){
return *this;
}
else if (this->str() == ""){
m_parse_success = false;
return *this;
}
else if (!(std::isalpha(m_str[m_parse_begin]))){
m_parse_success = false;
return *this;
}
for (size_t ct = 0; m_begin < m_end; m_begin++){
//std::cout << "m_str[" << m_begin << "] == " << m_str[m_begin] << '\n';//DEBUG
if (std::isalpha(m_str[m_begin])){
ct += 1;
}
if (ct > 0 && m_begin == m_end){
m_parse_success = true;
}
}
return *this;
|
also, some more description:
Proposed substring design favors pattern recognition and disambiguation, while the std::string is more of a text placeholder with built-in functionality for search and text mutation.
substring::parse( ) is the character scan initialization.
substring::isalpha( ) is the scan.
substring::succeeded( ) is the way to get boolean answer to whether the scan has passed ok.
Once the boundaries of the substring are initially set, the parsing can take various logical paths. If one would like to assure that the result of the scan has consumed the entire substring, they can use the substring::isempty( ) test at the end:
if ( sub.parse().isaplha().isempty().succeeded() == true ) //...
Due to rolling m_begin forward to m_end the substring will remain at an empty status once the entire sequence of characters was parsed successfully.
Since failed scan would inevitably obliterate the substring, substring::unparse( ) function allows program to recover and try an alternative character matching pattern.
Character test functions consume characters starting at the position m_begin. If characters pass the isalpha(ch) test, m_begin moves forward. For example,
sub.parse().isaplha().succeeded()
returns true if there is at least one alphabetic character at the beginning of the substring.
----------------------------------------------------------------------------------------------------------------------
I think this version works. Does anyone see anything wrong?
Full program code here:
http://cplusplus.com/forum/beginner/58036/