Debian Codegraph @

src/pandoc.cpp

raw symbols SCIP
1 #include <Rcpp.h>
2 #include <string.h>
3 using namespace Rcpp;
4
5 // simplified version of format that fits the need for table.expand
6 // checks justify param and adds needed number of whitespaces based on it
7 std::string format_cpp(const std::string &x, const std::string &justify, int width) {
8 std::string result = "";
9 Rcpp::Function nchar("nchar");
10 int xlen = 0;
11 // const char *s = x.c_str();
12 // while (*s) xlen += (*s++ & 0xc0) != 0x80;
13 xlen = as<int>(nchar(x, Rcpp::Named("type") = "width"));
14 if (justify == "left"){
15 result += x;
16 result += std::string(width - xlen, ' ');
17 } else if (justify == "right"){
18 result += std::string(width - xlen, ' ');
19 result += x;
20 } else {
21 result += std::string((width - xlen)/2, ' ');
22 result += x;
23 for (int j = (width + xlen)/2; j < width; j++){
24 result += " ";
25 }
26 }
27 return result;
28 }
29
30 //[[Rcpp::export]]
31 std::string tableExpand_cpp(CharacterVector cells, IntegerVector colsWidth, CharacterVector justify, CharacterVector sepCols, std::string style) {
32 std::string res = "", word, *line;
33 size_t pos;
34 char endline = '\n';
35 std::vector<std::string> cellsC(cells.size()), *resSplit;
36 int i, j, n = 0;
37 std::size_t maxLengthCells = 0;
38 bool hasLineBreak = false;
39
40 // check for having a line break and convert to string vector for easiness
41 for (std::size_t i = 0; i < cellsC.size(); i++){
42 cellsC[i] = std::string(cells[i]);
43 if (cellsC[i].find(endline) != std::string::npos)
44 hasLineBreak = true;
45 }
46
47 if (hasLineBreak){
48 // in case of line break, we need to recursively call tableExpand_cpp
49 // before that we need to find number of resulting calls (max number of line breaks) and prepare arguments for recursive calls
50 if (style == "simple" || style == "rmarkdown")
51 stop("Pandoc does not support newlines in simple or Rmarkdown table format!");
52 n = cellsC.size();
53 resSplit = new std::vector<std::string>[n];
54 // find max number of line breaks by going from cell to cell
55 for (i = 0; i < n; i++){
56 line = &cellsC[i];
57 do {
58 pos = line->find(endline);
59 if (line->substr(0, pos) != "")
60 resSplit[i].push_back(line->substr(0, pos));
61 line->erase(0, pos + 1);
62 }
63 while (pos != std::string::npos);
64 if (resSplit[i].size() > maxLengthCells)
65 maxLengthCells = resSplit[i].size();
66 }
67 // prepare arguments and do a recursive call
68 for (std::size_t i = 0; i < maxLengthCells; i++){
69 CharacterVector newCells(n);
70 for (j = 0; j < n; j++)
71 newCells[j] = resSplit[j].size() > i ? resSplit[j][i] : " ";
72 res += tableExpand_cpp(newCells, colsWidth, justify, sepCols, style);
73 if (i != (maxLengthCells - 1)) // because of collapse usage
74 res += '\n';
75 }
76 } else {
77 // in case of no line breaks format each cell (add needed number of whitespaces and justify) and concatenate cells
78 res = sepCols[0];
79 for (i = 0; i < cells.size(); i++){
80 res += format_cpp(as<std::string>(cells[i]), as<std::string>(justify[i]), colsWidth[i]);
81 if (i != (cells.length() - 1)) // because of collapse usage
82 res += sepCols[1];
83 }
84 res += sepCols[2];
85 }
86 return res;
87 }