remove lib_list.hh and merge all to lib_list.cc

master
Daniel Kolesa 2016-08-11 17:26:03 +01:00
parent cfb3a816bb
commit a8b6843a88
4 changed files with 149 additions and 161 deletions

View File

@ -27,7 +27,7 @@ $(LIBCS_LIB): $(LIBCS_OBJ)
clean:
rm -f $(LIBCS_LIB) $(LIBCS_OBJ)
cubescript.o: cubescript.hh lib_list.hh
cubescript.o: cubescript.hh
lib_str.o: cubescript.hh
lib_math.o: cubescript.hh
lib_list.o: cubescript.hh lib_list.hh
lib_list.o: cubescript.hh

View File

@ -1,5 +1,4 @@
#include "cubescript.hh"
#include "lib_list.hh"
#include <limits.h>
#include <ctype.h>
@ -3520,34 +3519,6 @@ bool CsState::run_file(ostd::ConstCharRange fname) {
return true;
}
namespace util {
ostd::Size list_length(ostd::ConstCharRange s) {
ListParser p(s);
ostd::Size ret = 0;
while (p.parse()) ++ret;
return ret;
}
ostd::Maybe<ostd::String> list_index(ostd::ConstCharRange s,
ostd::Size idx) {
ListParser p(s);
for (ostd::Size i = 0; i < idx; ++i)
if (!p.parse()) return ostd::nothing;
if (!p.parse())
return ostd::nothing;
return ostd::move(p.element());
}
ostd::Vector<ostd::String> list_explode(ostd::ConstCharRange s,
ostd::Size limit) {
ostd::Vector<ostd::String> ret;
ListParser p(s);
while ((ret.size() < limit) && p.parse())
ret.push(ostd::move(p.element()));
return ret;
}
}
void cs_init_lib_io(CsState &cs) {
cs_add_command(cs, "exec", "sb", [&cs](TvalRange args) {
auto file = args[0].get_strr();

View File

@ -1,11 +1,157 @@
#include "cubescript.hh"
#include "lib_list.hh"
namespace cscript {
char *cs_dup_ostr(ostd::ConstCharRange s);
int cs_parse_int(ostd::ConstCharRange s);
float cs_parse_float(ostd::ConstCharRange s);
ostd::ConstCharRange cs_parse_str(ostd::ConstCharRange str);
char const *parseword(char const *p);
struct ListParser {
ostd::ConstCharRange input;
ostd::ConstCharRange quote = ostd::ConstCharRange();
ostd::ConstCharRange item = ostd::ConstCharRange();
ListParser() = delete;
ListParser(ostd::ConstCharRange src): input(src) {}
void skip() {
for (;;) {
while (!input.empty()) {
char c = input.front();
if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n'))
input.pop_front();
else
break;
}
if ((input.size() < 2) || (input[0] != '/') || (input[1] != '/'))
break;
input = ostd::find(input, '\n');
}
}
bool parse() {
skip();
if (input.empty())
return false;
switch (input.front()) {
case '"':
quote = input;
input.pop_front();
item = input;
input = cs_parse_str(input);
item = ostd::slice_until(item, input);
if (!input.empty() && (input.front() == '"'))
input.pop_front();
quote = ostd::slice_until(quote, input);
break;
case '(':
case '[': {
quote = input;
input.pop_front();
item = input;
char btype = quote.front();
int brak = 1;
for (;;) {
input = ostd::find_one_of(input,
ostd::ConstCharRange("\"/;()[]"));
if (input.empty())
return true;
char c = input.front();
input.pop_front();
switch (c) {
case '"':
input = cs_parse_str(input);
if (!input.empty() && (input.front() == '"'))
input.pop_front();
break;
case '/':
if (!input.empty() && (input.front() == '/'))
input = ostd::find(input, '\n');
break;
case '(':
case '[':
brak += (c == btype);
break;
case ')':
if ((btype == '(') && (--brak <= 0))
goto endblock;
break;
case ']':
if ((btype == '[') && (--brak <= 0))
goto endblock;
break;
}
}
endblock:
item = ostd::slice_until(item, input);
item.pop_back();
quote = ostd::slice_until(quote, input);
break;
}
case ')':
case ']':
return false;
default: {
char const *e = parseword(input.data());
item = input;
input.pop_front_n(e - input.data());
item = ostd::slice_until(item, input);
quote = item;
break;
}
}
skip();
if (!input.empty() && (input.front() == ';'))
input.pop_front();
return true;
}
ostd::String element() {
ostd::String s;
s.reserve(item.size());
if (!quote.empty() && (quote.front() == '"')) {
auto writer = s.iter_cap();
util::unescape_string(writer, item);
writer.put('\0');
} else {
memcpy(s.data(), item.data(), item.size());
s[item.size()] = '\0';
}
s.advance(item.size());
return s;
}
};
namespace util {
ostd::Size list_length(ostd::ConstCharRange s) {
ListParser p(s);
ostd::Size ret = 0;
while (p.parse()) ++ret;
return ret;
}
ostd::Maybe<ostd::String> list_index(ostd::ConstCharRange s,
ostd::Size idx) {
ListParser p(s);
for (ostd::Size i = 0; i < idx; ++i)
if (!p.parse()) return ostd::nothing;
if (!p.parse())
return ostd::nothing;
return ostd::move(p.element());
}
ostd::Vector<ostd::String> list_explode(ostd::ConstCharRange s,
ostd::Size limit) {
ostd::Vector<ostd::String> ret;
ListParser p(s);
while ((ret.size() < limit) && p.parse())
ret.push(ostd::move(p.element()));
return ret;
}
}
struct NullValue: TaggedValue {
NullValue() { set_null(); }

View File

@ -1,129 +0,0 @@
#ifndef LIB_LIST_HH
#define LIB_LIST_HH
#include "cubescript.hh"
namespace cscript {
ostd::ConstCharRange cs_parse_str(ostd::ConstCharRange str);
char const *parseword(char const *p);
struct ListParser {
ostd::ConstCharRange input;
ostd::ConstCharRange quote = ostd::ConstCharRange();
ostd::ConstCharRange item = ostd::ConstCharRange();
ListParser() = delete;
ListParser(ostd::ConstCharRange src): input(src) {}
void skip() {
for (;;) {
while (!input.empty()) {
char c = input.front();
if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n'))
input.pop_front();
else
break;
}
if ((input.size() < 2) || (input[0] != '/') || (input[1] != '/'))
break;
input = ostd::find(input, '\n');
}
}
bool parse() {
skip();
if (input.empty())
return false;
switch (input.front()) {
case '"':
quote = input;
input.pop_front();
item = input;
input = cs_parse_str(input);
item = ostd::slice_until(item, input);
if (!input.empty() && (input.front() == '"'))
input.pop_front();
quote = ostd::slice_until(quote, input);
break;
case '(':
case '[': {
quote = input;
input.pop_front();
item = input;
char btype = quote.front();
int brak = 1;
for (;;) {
input = ostd::find_one_of(input,
ostd::ConstCharRange("\"/;()[]"));
if (input.empty())
return true;
char c = input.front();
input.pop_front();
switch (c) {
case '"':
input = cs_parse_str(input);
if (!input.empty() && (input.front() == '"'))
input.pop_front();
break;
case '/':
if (!input.empty() && (input.front() == '/'))
input = ostd::find(input, '\n');
break;
case '(':
case '[':
brak += (c == btype);
break;
case ')':
if ((btype == '(') && (--brak <= 0))
goto endblock;
break;
case ']':
if ((btype == '[') && (--brak <= 0))
goto endblock;
break;
}
}
endblock:
item = ostd::slice_until(item, input);
item.pop_back();
quote = ostd::slice_until(quote, input);
break;
}
case ')':
case ']':
return false;
default: {
char const *e = parseword(input.data());
item = input;
input.pop_front_n(e - input.data());
item = ostd::slice_until(item, input);
quote = item;
break;
}
}
skip();
if (!input.empty() && (input.front() == ';'))
input.pop_front();
return true;
}
ostd::String element() {
ostd::String s;
s.reserve(item.size());
if (!quote.empty() && (quote.front() == '"')) {
auto writer = s.iter_cap();
util::unescape_string(writer, item);
writer.put('\0');
} else {
memcpy(s.data(), item.data(), item.size());
s[item.size()] = '\0';
}
s.advance(item.size());
return s;
}
};
} /*namespace cscript */
#endif