Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. void MyString::cleanUpPaddedHyphens(std::string &string_OUT)
  2. {
  3.     //run cleanUpExtraSpaces() before cleaning padded hyphens.
  4.     size_t index {0};
  5.     std::vector<size_t> indexes_to_erase {};
  6.     //search loop
  7.     for (const char &character : string_OUT)
  8.     {
  9.         //look for an occurance of a digit followed by a space followed by a hyphen '5 -'
  10.         if (isdigit(character) && string_OUT.substr(index + 1, 2) == " -")
  11.         {
  12.             //pre-space detected, add it to the erase list
  13.             indexes_to_erase.push_back(index + 1); // +1 because the space is one character after the index
  14.         }
  15.         //look for an occurance of a hyphen followed by a space followed by a digit '- 7'
  16.         if (string_OUT.substr(index, 2) == "- " && isdigit(string_OUT[index + 2]))
  17.         {
  18.             //post-space detected, add it to the erase list
  19.             indexes_to_erase.push_back(index + 1);
  20.         }
  21.         index++;
  22.     }
  23.     //clean string from back to front to keep index numbers valid
  24.     while (indexes_to_erase.empty() == false)
  25.     {
  26.         index = indexes_to_erase.back();
  27.         indexes_to_erase.pop_back();
  28.         string_OUT.erase(index, 1);
  29.         //diag
  30.         //std::cout << "\nremoving space at " << index << "\n";
  31.         //end diag
  32.     }
  33.     //diag
  34.     //std::cout << "\n" << string_OUT << "\n";
  35.     //end diag
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement