Skip to content

Commit

Permalink
Add utils::vertical_split()
Browse files Browse the repository at this point in the history
  • Loading branch information
jyrkive committed May 4, 2017
1 parent e32bb5e commit afcca42
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/serialization/string_utils.cpp
Expand Up @@ -26,6 +26,7 @@
#include "utils/general.hpp"
#include <cassert>
#include <array>
#include <stdexcept>

#include <boost/algorithm/string.hpp>

Expand Down Expand Up @@ -381,6 +382,35 @@ std::vector<std::string> parenthetical_split(const std::string& val,
return res;
}

std::pair<string_view, string_view> vertical_split(const std::string& val)
{
// Count the number of lines.
int num_lines = std::count(val.begin(), val.end(), '\n') + 1;

if(num_lines < 2) {
throw std::logic_error("utils::vertical_split: the string contains only one line");
}

// Split the string at the point where we have encountered
// (number of lines / 2 - 1) line separators.
int split_point = 0;
int num_found_line_separators = 0;
for(size_t i = 0; i < val.size(); ++i) {
if(val[i] == '\n') {
++num_found_line_separators;
if(num_found_line_separators >= num_lines / 2 - 1) {
split_point = i;
break;
}
}
}

assert(split_point != 0);

return { string_view(val, 0, split_point),
string_view(val, split_point + 1, val.size() - (split_point + 1)) };
}

// Modify a number by string representing integer difference, or optionally %
int apply_modifier( const int number, const std::string &amount, const int minimum ) {
// wassert( amount.empty() == false );
Expand Down
42 changes: 42 additions & 0 deletions src/serialization/string_utils.hpp
Expand Up @@ -23,6 +23,7 @@
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

class t_string;
Expand All @@ -31,6 +32,34 @@ namespace utils {

using string_map = std::map<std::string, t_string>;

// TODO: when https://github.com/aquileia/external has boost::string_view,
// replace this class with it.
class string_view
{
public:
const char* str;
const int size;

string_view(const std::string& str_, int offset, int size_)
: str(&str_[offset])
, size(size_)
{
}

string_view(const std::string& str_)
: str(str_.c_str())
, size(str_.size())
{
}

string_view(const string_view&) = default;

explicit operator std::string() const
{
return std::string(str, size);
}
};

bool isnewline(const char c);
bool portable_isspace(const char c);
bool notspace(char c);
Expand Down Expand Up @@ -145,6 +174,19 @@ std::vector<std::string> square_parenthetical_split(
const std::string& right = ")]",
const int flags = REMOVE_EMPTY | STRIP_SPACES);

/**
* Splits a string into two parts as evenly as possible based on lines.
* For example, if the string contains 3288 lines, then both parts will
* be 1644 lines long.
*
* The line separator in between won't be in either of the parts the
* function returns.
*
* Because this function is intended for extremely long strings
* (kilobytes long), it returns string_views for performance.
*/
std::pair<string_view, string_view> vertical_split(const std::string& val);

/**
* Generates a new string joining container items in a list.
*
Expand Down

0 comments on commit afcca42

Please sign in to comment.