Debian Codegraph @

src/helpers.cpp

raw symbols SCIP
1 #include <Rcpp.h>
2 #include <string>
3
4 using namespace std;
5
6 // wrapper around concatenation. Based on string given decides of line break is needed and how to insert a separator
7 // pos is a check of paste_wrapper is called in the end of tokenization
8 void paste_wrapper(std::string& result, std::string& s, char sep, size_t pos) {
9 char last = *result.rbegin(); // last character in result
10 if (result.empty() || last == '\n'){
11 result += s;
12 result = (pos != std::string::npos) ? result + '\n' : result;
13 } else {
14 result += sep + s;
15 }
16 }
17
18 // helper to process word hyphenation. Hyphenates word and adds it to the result also respectively changing line width
19 void process_hyphenation(std::string &result, std::string &word, int &lline, int max_width, Rcpp::Function hyphen) {
20 char chyphen = '-';
21 Rcpp::CharacterVector hyphened_word_vec;
22 std::string hyphened_word, syllable;
23 int lsyl, hyph, lword = word.length(); // l is short for length (syl length and word length)
24 size_t pos = std::string::npos;
25
26 // roll line length back and hyphenate word
27 lline = lline - lword;
28 hyphened_word_vec = hyphen(word);
29 hyphened_word = std::string(hyphened_word_vec[0]);
30
31 // start tokenization by hyphens
32 pos = hyphened_word.find(chyphen);
33 if (pos == std::string::npos){ // no hyphenation is possible
34 if (lline == 0){ // need to and something to an empty line
35 result += word + '\n';
36 } else { // add word at the next line
37 result += '\n' + word;
38 lline = lword;
39 }
40 } else {
41 do {
42 pos = hyphened_word.find(chyphen);
43 syllable = hyphened_word.substr(0, pos);
44 lsyl = syllable.length();
45 hyph = (pos != std::string::npos) ? 1 : 0; // check if hyphen will be needed (in case of end - no, yes in other case)
46 if (lsyl + lline + hyph > max_width){
47 if (lline == 0) // must add at least something
48 result += syllable;
49 else // if syllable is last possible syllable no hyphen is needed, in other case add hyphen
50 result = (word.substr(0, syllable.length()) == syllable) ? (result + '\n' + syllable) : (result + "-\n" + syllable);
51 lline = lsyl;
52 } else {
53 // if syllable is last possible syllable and line is not empty than we need a whitespace
54 result = (word.substr(0, syllable.length()) == syllable && lline != 0) ? result + ' ' : result;
55 result += syllable;
56 lline += lsyl;
57 }
58 hyphened_word.erase(0, pos + 1);
59 } while (pos != std::string::npos);
60 }
61 }
62
63 //[[Rcpp::export]]
64 std::string splitLine_cpp(std::string str, int max_width, bool use_hyphening, Rcpp::Function hyphen) {
65 char whitespace = ' ';
66 size_t pos;
67 std::string result, word;
68 int lline = 0, lword; // l is short for length (line length and word length)
69
70 // break line into tokens by whitespace and iterate through those tokens
71 do {
72 pos = str.find(whitespace);
73 word = str.substr(0, pos);
74 lword = word.length();
75 lline = lline + lword;
76 if (lline > max_width) {
77 if (!use_hyphening) {
78 paste_wrapper(result, word, '\n', pos);
79 lline = word.length();
80 } else process_hyphenation(result, word, lline, max_width, hyphen);
81 } else{
82 paste_wrapper(result, word, ' ', std::string::npos);
83 }
84 lline += 1;
85 str.erase(0, pos + 1);
86 } while (pos != std::string::npos);
87
88 return result;
89 }