1

I am porting lua to chaiscript. The original lua code uses split:

function string:split(delimiter)
    local result = { }
    local from  = 1
    local delim_from, delim_to = string.find( self, delimiter, from  )
    while delim_from do
        table.insert( result, string.sub( self, from , delim_from-1 ) )
        from  = delim_to + 1
        delim_from, delim_to = string.find( self, delimiter, from  )
    end
    table.insert( result, string.sub( self, from  ) )
    return result
end

But I can't find chaiscript's version of split... Does chaiscript have a split function?

tmthydvnprt
  • 10,398
  • 8
  • 52
  • 72
Kokoas
  • 9
  • 3

2 Answers2

2

ChaiScript's string functionality is a direct mapping of what is available in C++, std::string. So, no there is no built in split capability.

lefticus
  • 3,346
  • 2
  • 24
  • 28
0

You can easily use your own one like the one found here: Split a string in C++?

#include <string>
#include <sstream>
#include <vector>

void split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss;
    ss.str(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
}


std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}
Community
  • 1
  • 1
aggsol
  • 2,343
  • 1
  • 32
  • 49