Skip to content

Instantly share code, notes, and snippets.

@paulll
Last active September 15, 2015 21:03
Show Gist options
  • Save paulll/e98442346b00cd6f47ce to your computer and use it in GitHub Desktop.
Save paulll/e98442346b00cd6f47ce to your computer and use it in GitHub Desktop.
Проверка, можно ли составить строку из множеств подстрок (в том числе и вложенных подстрок)
"use strict";
function TokenList (rest, part) {
this.rest = rest.rest.substr(part.length);
this.stack = rest.stack.concat([part]);
this.substr = String.prototype.substr.bind(this.rest);
this.indexOf = String.prototype.indexOf.bind(this.rest);
}
function Input (start) {
this.rest = start;
this.stack = [];
this.substr = String.prototype.substr.bind(this.rest);
this.indexOf = String.prototype.indexOf.bind(this.rest);
}
function* check (rest, tree) {
if (tree instanceof Set) {
for (let value of tree) {
if (typeof value === 'string') {
if (rest.indexOf(value) === 0) {
yield new TokenList(rest, value);
}
} else {
yield* check(rest, value);
}
}
} else {
if (typeof tree[0] === 'string') {
if (rest.indexOf(tree[0]) === 0) {
if (tree.length - 1) {
yield* check(new TokenList(rest, tree[0]), tree.slice(1));
} else {
yield new TokenList(rest, tree[0]);
}
}
} else {
for (let str of check(rest, tree[0])) {
if (tree.length - 1) {
yield* check(str, tree.slice(1));
} else {
yield str;
}
}
}
}
}
function restore(k, v) {
if (v.hasOwnProperty('v')) {
return new Set(v.v);
}
return v;
}
/**
* Пример
*/
var code = '{"v":[[{"v":["a",[{"v":["","b"]},"e"],"c"]}," ",{"v":["a","b","c"]}," c"]]}',
ast = JSON.parse(code, restore);
//console.log(require('util').inspect(ast, false, 10));
if (check('be b c', ast).next().value.rest == '') {
console.log('yes')
} else {
console.log('no');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment