What is the exact feature or behavior required?
Onestring currently has a method substr() that, with a single parameter, will remove the given quantity of characters from the front. So, given onestring test_str = "012345689";, then the command test_str.substr(test_str.length() -1) returns a onestring containing just "9".
It would be nice if test_str.substr(test_str.length()) returned an empty onestring, rather than throwing an out of range error. That way, operations up to and including the length of the initial onestring can complete successfully without extra comparisons in external code.
One or more example use cases for the feature, including sample expected input and output.
I have to jump through extra hoops often in SIMPLEXpress to use onestrings in lexing, for example this code at lines 75-82 of simplex.cpp (in both cases, the buffer object is a onestring):
// Then remove the matched amount from the front. if (static_cast<int>(buffer.length()) >= matched) { if (static_cast<int>(buffer.length()) == matched) { buffer.clear(); } else { onestring new_buffer = buffer.substr(matched); buffer = new_buffer; }
or this similar snippet at 298-304:
} else { if (buffer.length() <= 1) { buffer.clear(); } else { buffer = buffer.substr(1); } }
An explanation of why the feature is needed. This is important to prioritization. “It would be nice if...” takes a backseat to “This common scenario doesn’t work without...”.
It makes logical sense that, if removing (length - 1) characters from a string returns one character, then removing (length) characters would return an empty string. I've been told removing (length) characters throwing an error and terminating the application is not a bug, but it feels like one to me.
Any initial ideas you have on implementation (if applicable).
Modifying line 492 in onestring.hpp:
if (pos >= this->_elements) {
to
if (pos > this->_elements) {
actually seems to do the job just fine, my test code works, all onestring's included tests pass, but I don't know if there are any additional considerations to that modification that I'm missing.