PIMapIterator

small PIString optimize
general PIChunkStream pack optimization
This commit is contained in:
2020-08-02 15:57:21 +03:00
parent 1fb5356825
commit b468a6d581
7 changed files with 253 additions and 78 deletions

View File

@@ -108,7 +108,7 @@ void PICodeParser::parseFile(const PIString & file, bool follow_includes) {
piForeachC (Enum & c, enums) { piForeachC (Enum & c, enums) {
piCout << PIStringAscii("enum") << c.name << c.meta; piCout << PIStringAscii("enum") << c.name << c.meta;
piForeachC (EnumeratorInfo & e, c.members) piForeachC (EnumeratorInfo & e, c.members)
piCout << " " << e.name << "=" << e.value << e.meta; piCout << " " << e.name << '=' << e.value << e.meta;
} }
piCout << "\n\nTypedefs:"; piCout << "\n\nTypedefs:";
piForeachC (Typedef & c, typedefs) piForeachC (Typedef & c, typedefs)
@@ -277,11 +277,11 @@ bool PICodeParser::parseFileContent(PIString & fc, bool main) {
int nl = pfc.size_s(); int nl = pfc.size_s();
if (pl == nl) break; if (pl == nl) break;
pl = nl; pl = nl;
if (pfc.left(9) == "namespace") { if (pfc.left(9) == PIStringAscii("namespace")) {
pfc.cutLeft(pfc.find('{') + 1); pfc.cutLeft(pfc.find('{') + 1);
continue; continue;
} }
if (pfc.left(8) == "template") { if (pfc.left(8) == PIStringAscii("template")) {
pfc.cutLeft(8); pfc.cutLeft(8);
pfc.takeRange('<', '>'); pfc.takeRange('<', '>');
bool def = !isDeclaration(pfc, 0, &end); bool def = !isDeclaration(pfc, 0, &end);
@@ -290,11 +290,11 @@ bool PICodeParser::parseFileContent(PIString & fc, bool main) {
else pfc.takeSymbol(); else pfc.takeSymbol();
continue; continue;
} }
if (pfc.left(5) == PIStringAscii("class") || pfc.left(6) == "struct" || pfc.left(5) == "union") { if (pfc.left(5) == PIStringAscii("class") || pfc.left(6) == PIStringAscii("struct") || pfc.left(5) == PIStringAscii("union")) {
int dind = pfc.find('{', 0), find = pfc.find(';', 0); int dind = pfc.find('{', 0), find = pfc.find(';', 0);
if (dind < 0 && find < 0) {pfc.cutLeft(6); continue;} if (dind < 0 && find < 0) {pfc.cutLeft(6); continue;}
if (dind < 0 || find < dind) {pfc.cutLeft(6); continue;} if (dind < 0 || find < dind) {pfc.cutLeft(6); continue;}
ccmn = pfc.left(dind) + "{\n" + pfc.mid(dind).takeRange('{', '}') + "\n}\n"; ccmn = pfc.left(dind) + PIStringAscii("{\n") + pfc.mid(dind).takeRange('{', '}') + PIStringAscii("\n}\n");
pfc.remove(0, ccmn.size()); pfc.remove(0, ccmn.size());
parseClass(0, ccmn); parseClass(0, ccmn);
continue; continue;
@@ -363,7 +363,7 @@ PICodeParser::Entity * PICodeParser::parseClassDeclaration(const PIString & fc)
cur_def_vis = (is_class ? Private : Public); cur_def_vis = (is_class ? Private : Public);
PIString cn = cd.mid(6).trim(); PIString cn = cd.mid(6).trim();
bool has_name = !cn.isEmpty(); bool has_name = !cn.isEmpty();
if (cn.isEmpty()) cn = "<unnamed_" + PIString::fromNumber(anon_num++) + '>'; if (cn.isEmpty()) cn = PIStringAscii("<unnamed_") + PIString::fromNumber(anon_num++) + '>';
//piCout << "found " << typename_ << cn; //piCout << "found " << typename_ << cn;
if (cn.isEmpty()) return 0; if (cn.isEmpty()) return 0;
Entity * e = new Entity(); Entity * e = new Entity();
@@ -393,7 +393,7 @@ PIString PICodeParser::parseClass(Entity * parent, PIString & fc) {
int ps = -1; int ps = -1;
bool def = false; bool def = false;
PIString prev_namespace = cur_namespace, stmp; PIString prev_namespace = cur_namespace, stmp;
cur_namespace = ce->name + "::"; cur_namespace = ce->name + PIStringAscii("::");
//piCout << "parse class" << ce->name << "namespace" << cur_namespace; //piCout << "parse class" << ce->name << "namespace" << cur_namespace;
//piCout << "\nparse class" << ce->name << "namespace" << cur_namespace; //piCout << "\nparse class" << ce->name << "namespace" << cur_namespace;
while (!fc.isEmpty()) { while (!fc.isEmpty()) {
@@ -425,7 +425,7 @@ PIString PICodeParser::parseClass(Entity * parent, PIString & fc) {
} }
if (cw == PIStringAscii("friend")) {fc.cutLeft(fc.find(';') + 1); continue;} if (cw == PIStringAscii("friend")) {fc.cutLeft(fc.find(';') + 1); continue;}
if (cw == PIStringAscii("typedef")) {ce->typedefs << parseTypedef(fc.takeLeft(fc.find(';'))); typedefs << ce->typedefs.back(); typedefs.back().first.insert(0, cur_namespace); if (ce->typedefs.back().first.isEmpty()) ce->typedefs.pop_back(); fc.takeSymbol(); continue;} if (cw == PIStringAscii("typedef")) {ce->typedefs << parseTypedef(fc.takeLeft(fc.find(';'))); typedefs << ce->typedefs.back(); typedefs.back().first.insert(0, cur_namespace); if (ce->typedefs.back().first.isEmpty()) ce->typedefs.pop_back(); fc.takeSymbol(); continue;}
if (cw == "template") { if (cw == PIStringAscii("template")) {
fc.takeRange('<', '>'); fc.takeRange('<', '>');
def = !isDeclaration(fc, 0, &end); def = !isDeclaration(fc, 0, &end);
fc.cutLeft(end); fc.cutLeft(end);
@@ -453,7 +453,7 @@ PICodeParser::MetaMap PICodeParser::parseMeta(PIString & fc) {
if (fc.isEmpty()) return ret; if (fc.isEmpty()) return ret;
PIStringList ml = fc.split(','); PIStringList ml = fc.split(',');
piForeachC (PIString & m, ml) { piForeachC (PIString & m, ml) {
int i = m.find("="); int i = m.find('=');
if (i < 0) continue; if (i < 0) continue;
PIString mv = m.mid(i + 1).trim(); PIString mv = m.mid(i + 1).trim();
if (mv.startsWith('\"')) mv.cutLeft(1); if (mv.startsWith('\"')) mv.cutLeft(1);
@@ -466,6 +466,8 @@ PICodeParser::MetaMap PICodeParser::parseMeta(PIString & fc) {
bool PICodeParser::parseEnum(Entity * parent, const PIString & name, PIString fc, const MetaMap & meta) { bool PICodeParser::parseEnum(Entity * parent, const PIString & name, PIString fc, const MetaMap & meta) {
static const PIString s_ss = PIStringAscii(" ");
static const PIString s_M = PIStringAscii("$M");
//piCout << PIStringAscii("enum") << name << fc; //piCout << PIStringAscii("enum") << name << fc;
Enum e(name); Enum e(name);
e.meta = meta; e.meta = meta;
@@ -474,12 +476,12 @@ bool PICodeParser::parseEnum(Entity * parent, const PIString & name, PIString fc
int cv = -1, ind = 0; int cv = -1, ind = 0;
piForeach (PIString & v, vl) { piForeach (PIString & v, vl) {
MetaMap meta; MetaMap meta;
int mi = v.find(PIStringAscii("$M")); int mi = v.find(s_M);
if (mi >= 0) { if (mi >= 0) {
meta = tmp_meta.value(v.takeMid(mi, 5)); meta = tmp_meta.value(v.takeMid(mi, 5));
v.replaceAll(" ", ' '); v.replaceAll(s_ss, ' ');
} }
vn = v; ind = v.find("="); vn = v; ind = v.find('=');
if (ind > 0) {cv = v.right(v.size_s() - ind - 1).toInt(); vn = v.left(ind);} if (ind > 0) {cv = v.right(v.size_s() - ind - 1).toInt(); vn = v.left(ind);}
if (ind < 0) ++cv; if (ind < 0) ++cv;
e.members << EnumeratorInfo(vn.trim(), cv, meta); e.members << EnumeratorInfo(vn.trim(), cv, meta);
@@ -512,8 +514,27 @@ PICodeParser::Typedef PICodeParser::parseTypedef(PIString fc) {
bool PICodeParser::parseMember(Entity * parent, PIString & fc) { bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
static const PIString s_operator = PIStringAscii("operator");
static const PIString s_ss = PIStringAscii(" ");
static const PIString s_cs = PIStringAscii(", ");
static const PIString s_sb = PIStringAscii(" (");
static const PIString s_sM = PIStringAscii(" $M");
static const PIString s_M = PIStringAscii("$M");
static const PIString s_T = PIStringAscii("$T");
static const PIString s_inline_s = PIStringAscii("inline ");
static const PIString s_static_s = PIStringAscii("static ");
static const PIString s_virtual_s = PIStringAscii("virtual ");
static const PIString s_void = PIStringAscii("void");
static const PIString s_using = PIStringAscii("using");
static const PIString s_s5 = PIStringAscii(" ");
static const PIString s_s_const_s = PIStringAscii(" const ");
static const PIString s_s_static_s = PIStringAscii(" static ");
static const PIString s_s_mutable_s = PIStringAscii(" mutable ");
static const PIString s_s_volatile_s = PIStringAscii(" volatile ");
static const PIString s_s_extern_s = PIStringAscii(" extern ");
if (fc.trim().isEmpty()) return true; if (fc.trim().isEmpty()) return true;
if (fc.find("operator") >= 0) return true; if (fc.find(s_operator) >= 0) return true;
tmp_temp.clear(); tmp_temp.clear();
//piCout << "parse member" << fc; //piCout << "parse member" << fc;
int ts = fc.find('<'), te = 0; int ts = fc.find('<'), te = 0;
@@ -521,22 +542,22 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
while (ts >= 0) { while (ts >= 0) {
ctemp = fc.mid(ts).takeRange('<', '>'); ctemp = fc.mid(ts).takeRange('<', '>');
if (ctemp.isEmpty()) {te = ts + 1; ts = fc.find('<', te); continue;} if (ctemp.isEmpty()) {te = ts + 1; ts = fc.find('<', te); continue;}
crepl = PIStringAscii("$T") + PIString::fromNumber(tmp_temp.size_s()).expandLeftTo(3, '0'); crepl = s_T + PIString::fromNumber(tmp_temp.size_s()).expandLeftTo(3, '0');
fc.replace(ts, ctemp.size_s() + 2, crepl); fc.replace(ts, ctemp.size_s() + 2, crepl);
tmp_temp[crepl] = '<' + ctemp + '>'; tmp_temp[crepl] = '<' + ctemp + '>';
ts = fc.find('<', te); ts = fc.find('<', te);
} }
fc.replaceAll('\n', ' ').replaceAll('\t', ' ').replaceAll(" ", ' ').replaceAll(", ", ',').replaceAll(PIStringAscii(" ("), '(').replaceAll(PIStringAscii(" $M"), PIStringAscii("$M")); fc.replaceAll('\n', ' ').replaceAll('\t', ' ').replaceAll(s_ss, ' ').replaceAll(s_cs, ',').replaceAll(s_sb, '(').replaceAll(s_sM, s_M);
//piCout << "parse member" << fc; //piCout << "parse member" << fc;
PIStringList tl, al; PIStringList tl, al;
Member me; Member me;
//piCout << fc; //piCout << fc;
if (fc.contains('(')) { if (fc.contains('(')) {
MetaMap meta; MetaMap meta;
int ind = fc.find(PIStringAscii("$M")); int ind = fc.find(s_M);
if (ind >= 0) { if (ind >= 0) {
meta = tmp_meta.value(fc.takeMid(ind, 5)); meta = tmp_meta.value(fc.takeMid(ind, 5));
fc.replaceAll(PIStringAscii(" "), ' ').replaceAll(PIStringAscii(" ("), '('); fc.replaceAll(s_ss, ' ').replaceAll(s_sb, '(');
} }
fc.cutRight(fc.size_s() - fc.findLast(')') - 1); fc.cutRight(fc.size_s() - fc.findLast(')') - 1);
te = fc.find('('); te = fc.find('(');
@@ -550,26 +571,26 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
me.arguments_full = fc.takeMid(ts + 2).cutRight(1).split(','); me.arguments_full = fc.takeMid(ts + 2).cutRight(1).split(',');
me.type = fc.cutRight(1).trim(); me.type = fc.cutRight(1).trim();
me.visibility = cur_def_vis; me.visibility = cur_def_vis;
if (me.type.find(PIStringAscii("inline ")) >= 0) { if (me.type.find(s_inline_s) >= 0) {
me.attributes |= Inline; me.attributes |= Inline;
me.type.removeAll(PIStringAscii("inline ")); me.type.removeAll(s_inline_s);
} }
if (me.type.find(PIStringAscii("static ")) >= 0) { if (me.type.find(s_static_s) >= 0) {
me.attributes |= Static; me.attributes |= Static;
me.type.removeAll(PIStringAscii("static ")); me.type.removeAll(s_static_s);
} }
if (me.type.find(PIStringAscii("virtual ")) >= 0) { if (me.type.find(s_virtual_s) >= 0) {
me.attributes |= Virtual; me.attributes |= Virtual;
me.type.removeAll(PIStringAscii("virtual ")); me.type.removeAll(s_virtual_s);
} }
normalizeEntityNamespace(me.type); normalizeEntityNamespace(me.type);
int i = 0; int i = 0;
//piCout << me.arguments_full; //piCout << me.arguments_full;
piForeach (PIString & a, me.arguments_full) piForeach (PIString & a, me.arguments_full)
if ((i = a.find("=")) > 0) if ((i = a.find('=')) > 0)
a.cutRight(a.size_s() - i).trim(); a.cutRight(a.size_s() - i).trim();
for (int j = 0; j < me.arguments_full.size_s(); ++j) for (int j = 0; j < me.arguments_full.size_s(); ++j)
if (me.arguments_full[j] == PIStringAscii("void")) { if (me.arguments_full[j] == s_void) {
me.arguments_full.remove(j); me.arguments_full.remove(j);
--j; --j;
} }
@@ -590,7 +611,7 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
parent->functions << me; parent->functions << me;
} else { } else {
if (fc.endsWith(';')) fc.cutRight(1); if (fc.endsWith(';')) fc.cutRight(1);
if (fc.startsWith(PIStringAscii("using")) || !(fc.contains(' ') || fc.contains('\t') || fc.contains('\n'))) return true; if (fc.startsWith(s_using) || !(fc.contains(' ') || fc.contains('\t') || fc.contains('\n'))) return true;
int bits = extractMemberBits(fc); int bits = extractMemberBits(fc);
tl = fc.split(','); tl = fc.split(',');
//piCout << "member" << fc << tl; //piCout << "member" << fc << tl;
@@ -599,8 +620,8 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
bool vn = true; bool vn = true;
ctemp = tl.front().trim(); ctemp = tl.front().trim();
PIString meta_t; PIString meta_t;
if (ctemp.contains(PIStringAscii("$M"))) if (ctemp.contains(s_M))
meta_t = ctemp.takeMid(ctemp.find(PIStringAscii("$M"))); meta_t = ctemp.takeMid(ctemp.find(s_M));
for (ts = ctemp.size_s() - 1; ts > 0; --ts) { for (ts = ctemp.size_s() - 1; ts > 0; --ts) {
if (vn) {if (!_isCChar(ctemp[ts]) && !ctemp[ts].isDigit() && ctemp[ts] != '[' && ctemp[ts] != ']') vn = false;} if (vn) {if (!_isCChar(ctemp[ts]) && !ctemp[ts].isDigit() && ctemp[ts] != '[' && ctemp[ts] != ']') vn = false;}
else {if (_isCChar(ctemp[ts]) || ctemp[ts].isDigit()) break;} else {if (_isCChar(ctemp[ts]) || ctemp[ts].isDigit()) break;}
@@ -609,26 +630,26 @@ bool PICodeParser::parseMember(Entity * parent, PIString & fc) {
me.visibility = cur_def_vis; me.visibility = cur_def_vis;
ctemp += meta_t; ctemp += meta_t;
restoreTmpTemp(&me); restoreTmpTemp(&me);
PIString type = PIStringAscii(" ") + me.type; PIString type = s_s5 + me.type;
if (type.find(PIStringAscii(" const ")) >= 0) { if (type.find(s_s_const_s) >= 0) {
me.attributes |= Const; me.attributes |= Const;
type.replaceAll(PIStringAscii(" const "), ' '); type.replaceAll(s_s_const_s, ' ');
} }
if (type.find(PIStringAscii(" static ")) >= 0) { if (type.find(s_s_static_s) >= 0) {
me.attributes |= Static; me.attributes |= Static;
type.replaceAll(PIStringAscii(" static "), ' '); type.replaceAll(s_s_static_s, ' ');
} }
if (type.find(PIStringAscii(" mutable ")) >= 0) { if (type.find(s_s_mutable_s) >= 0) {
me.attributes |= Mutable; me.attributes |= Mutable;
type.replaceAll(PIStringAscii(" mutable "), ' '); type.replaceAll(s_s_mutable_s, ' ');
} }
if (type.find(PIStringAscii(" volatile ")) >= 0) { if (type.find(s_s_volatile_s) >= 0) {
me.attributes |= Volatile; me.attributes |= Volatile;
type.replaceAll(PIStringAscii(" volatile "), ' '); type.replaceAll(s_s_volatile_s, ' ');
} }
if (type.find(PIStringAscii(" extern ")) >= 0) { if (type.find(s_s_extern_s) >= 0) {
me.attributes |= Extern; me.attributes |= Extern;
type.replaceAll(PIStringAscii(" extern "), ' '); type.replaceAll(s_s_extern_s, ' ');
} }
type.trim(); type.trim();
normalizeEntityNamespace(type); normalizeEntityNamespace(type);
@@ -677,6 +698,14 @@ int PICodeParser::extractMemberBits(PIString & fc) {
void PICodeParser::normalizeEntityNamespace(PIString & n) { void PICodeParser::normalizeEntityNamespace(PIString & n) {
static const PIString s_const_s = PIStringAscii("const ");
static const PIString s_static_s = PIStringAscii("static ");
static const PIString s_mutable_s = PIStringAscii("mutable ");
static const PIString s_volatile_s = PIStringAscii("volatile ");
static const PIString s_s_const_s = PIStringAscii(" const ");
static const PIString s_s_static_s = PIStringAscii(" static ");
static const PIString s_s_mutable_s = PIStringAscii(" mutable ");
static const PIString s_s_volatile_s = PIStringAscii(" volatile ");
PIString suff, pref; PIString suff, pref;
for (int i = n.size_s() - 1; i > 0; --i) for (int i = n.size_s() - 1; i > 0; --i)
if (_isCChar(n[i]) || n[i].isDigit()) { if (_isCChar(n[i]) || n[i].isDigit()) {
@@ -685,10 +714,10 @@ void PICodeParser::normalizeEntityNamespace(PIString & n) {
break; break;
} }
n.push_front(' '); n.push_front(' ');
if (n.find(PIStringAscii(" static ")) >= 0) {n.replaceAll(PIStringAscii(" static "), ""); pref += PIStringAscii("static ");} if (n.find(s_s_const_s) >= 0) {n.replaceAll(s_s_const_s, ""); pref += s_const_s;}
if (n.find(PIStringAscii(" const ")) >= 0) {n.replaceAll(PIStringAscii(" const "), ""); pref += PIStringAscii("const ");} if (n.find(s_s_static_s) >= 0) {n.replaceAll(s_s_static_s, ""); pref += s_static_s;}
if (n.find(PIStringAscii(" mutable ")) >= 0) {n.replaceAll(PIStringAscii(" mutable "), ""); pref += PIStringAscii("mutable ");} if (n.find(s_s_mutable_s) >= 0) {n.replaceAll(s_s_mutable_s, ""); pref += s_mutable_s;}
if (n.find(PIStringAscii(" volatile ")) >= 0) {n.replaceAll(PIStringAscii(" volatile "), ""); pref += PIStringAscii("volatile ");} if (n.find(s_s_volatile_s) >= 0) {n.replaceAll(s_s_volatile_s, ""); pref += s_volatile_s;}
n.trim(); n.trim();
int f = 0; int f = 0;
piForeachC (Entity * e, entities) { piForeachC (Entity * e, entities) {
@@ -697,28 +726,30 @@ void PICodeParser::normalizeEntityNamespace(PIString & n) {
return; return;
} }
if ((f = e->name.find(n)) >= 0) if ((f = e->name.find(n)) >= 0)
if (e->name.mid(f - 1, 1) == PIChar(':')) if (e->name.at(f - 1) == PIChar(':'))
if (e->name.find(cur_namespace) >= 0) { if (e->name.find(cur_namespace) >= 0) {
n = pref + e->name + suff; n = pref + e->name + suff;
return; return;
} }
} }
piForeachC (Enum & e, enums) piForeachC (Enum & e, enums) {
if ((f = e.name.find(n)) >= 0) if ((f = e.name.find(n)) >= 0)
if (e.name.mid(f - 1, 1) == PIChar(':')) if (e.name.at(f - 1) == PIChar(':'))
if (e.name.find(cur_namespace) >= 0) { if (e.name.find(cur_namespace) >= 0) {
//piCout << "change" << n << "to" << e.name + suff; //piCout << "change" << n << "to" << e.name + suff;
n = pref + e.name + suff; n = pref + e.name + suff;
return; return;
} }
piForeachC (Typedef & e, typedefs) }
piForeachC (Typedef & e, typedefs) {
if ((f = e.first.find(n)) >= 0) if ((f = e.first.find(n)) >= 0)
if (e.first.mid(f - 1, 1) == PIChar(':')) if (e.first.at(f - 1) == PIChar(':'))
if (e.first.find(cur_namespace) >= 0) { if (e.first.find(cur_namespace) >= 0) {
//piCout << "change" << n << "to" << e.name + suff; //piCout << "change" << n << "to" << e.name + suff;
n = pref + e.first + suff; n = pref + e.first + suff;
return; return;
} }
}
n = (pref + n + suff).trim(); n = (pref + n + suff).trim();
} }
@@ -864,9 +895,9 @@ bool PICodeParser::isDeclaration(const PIString & fc, int start, int * end) {
bool PICodeParser::isMainFile(const PIString & fc) { bool PICodeParser::isMainFile(const PIString & fc) {
int si = 0; int si = 0;
while (si >= 0) { while (si >= 0) {
int csi = fc.find(" main", si); int csi = fc.find(PIStringAscii(" main"), si);
if (csi < 0) csi = fc.find("\tmain", si); if (csi < 0) csi = fc.find(PIStringAscii("\tmain"), si);
if (csi < 0) csi = fc.find("\nmain", si); if (csi < 0) csi = fc.find(PIStringAscii("\nmain"), si);
if (csi < 0) return false; if (csi < 0) return false;
si = csi; si = csi;
int fi = fc.find('(', si + 5); int fi = fc.find('(', si + 5);
@@ -874,7 +905,7 @@ bool PICodeParser::isMainFile(const PIString & fc) {
if (fi - si < 10) { if (fi - si < 10) {
PIString ms(fc.mid(si, fi - si + 1)); PIString ms(fc.mid(si, fi - si + 1));
ms.removeAll(' ').removeAll('\t').removeAll('\n'); ms.removeAll(' ').removeAll('\t').removeAll('\n');
if (ms == "main(") return true; if (ms == PIStringAscii("main(")) return true;
} }
si += 5; si += 5;
} }
@@ -925,7 +956,7 @@ PIString PICodeParser::procMacros(PIString fc) {
} }
continue; continue;
} }
if (mif.left(4) == "else" && ifcnt == 0) { if (mif.left(4) == PIStringAscii("else") && ifcnt == 0) {
//piCout << "main else" << skip << grab; //piCout << "main else" << skip << grab;
if (grab) pfc << procMacros(nfc); if (grab) pfc << procMacros(nfc);
if (skip && !cond_ok) {skip = false; grab = true;} if (skip && !cond_ok) {skip = false; grab = true;}
@@ -959,7 +990,7 @@ bool PICodeParser::parseDirective(PIString d) {
if (d.isEmpty()) return true; if (d.isEmpty()) return true;
PIString dname = d.takeCWord(); PIString dname = d.takeCWord();
//piCout << "parseDirective" << d; //piCout << "parseDirective" << d;
if (dname == "include") { if (dname == PIStringAscii("include")) {
d.replaceAll('<', '\"').replaceAll('>', '\"'); d.replaceAll('<', '\"').replaceAll('>', '\"');
PIString cf = cur_file, ifc = d.takeRange('\"', '\"'); PIString cf = cur_file, ifc = d.takeRange('\"', '\"');
if (with_includes) { if (with_includes) {
@@ -982,7 +1013,7 @@ bool PICodeParser::parseDirective(PIString d) {
} }
return true; return true;
} }
if (dname == "undef") { if (dname == PIStringAscii("undef")) {
PIString mname = d.takeCWord(); PIString mname = d.takeCWord();
for (int i = 0; i < defines.size_s(); ++i) for (int i = 0; i < defines.size_s(); ++i)
if (defines[i].first == mname) {defines.remove(i); --i;} if (defines[i].first == mname) {defines.remove(i); --i;}

View File

@@ -67,10 +67,16 @@ void piQuickSort(T * a, ssize_t N) {
} }
} }
template <typename Key, typename T>
class PIMapIterator;
template <typename Key, typename T> template <typename Key, typename T>
class PIMap { class PIMap {
template <typename Key1, typename T1> friend PIByteArray & operator >>(PIByteArray & s, PIMap<Key1, T1> & v); template <typename Key1, typename T1> friend PIByteArray & operator >>(PIByteArray & s, PIMap<Key1, T1> & v);
template <typename Key1, typename T1> friend PIByteArray & operator <<(PIByteArray & s, const PIMap<Key1, T1> & v); template <typename Key1, typename T1> friend PIByteArray & operator <<(PIByteArray & s, const PIMap<Key1, T1> & v);
template <typename Key1, typename T1> friend class PIMapIterator;
public: public:
PIMap() {;} PIMap() {;}
PIMap(const PIMap<Key, T> & other) {*this = other;} PIMap(const PIMap<Key, T> & other) {*this = other;}
@@ -183,6 +189,9 @@ public:
const_reverse_iterator constRbegin() const {return const_reverse_iterator(this, size() - 1);} const_reverse_iterator constRbegin() const {return const_reverse_iterator(this, size() - 1);}
const_reverse_iterator constRend() const {return const_reverse_iterator(this, -1);} const_reverse_iterator constRend() const {return const_reverse_iterator(this, -1);}
PIMapIterator<Key, T> iterate() const {return PIMapIterator<Key, T>(*this);}
PIMapIterator<Key, T> riterate() const {return PIMapIterator<Key, T>(*this, true);}
size_t size() const {return pim_content.size();} size_t size() const {return pim_content.size();}
int size_s() const {return pim_content.size_s();} int size_s() const {return pim_content.size_s();}
size_t length() const {return pim_content.size();} size_t length() const {return pim_content.size();}
@@ -327,6 +336,41 @@ protected:
}; };
template <typename Key, typename T>
class PIMapIterator {
typedef PIMap<Key, T> MapType;
public:
PIMapIterator(const PIMap<Key, T> & map, bool reverse = false): m(map), pos(-1), rev(reverse) {
if (rev) pos = m.size_s();
}
const Key & key() const {return const_cast<MapType & >(m)._key(pos);}
const T & value() const {return const_cast<MapType & >(m)._value(pos);}
T & valueRef() const {return const_cast<MapType & >(m)._value(pos);}
inline bool hasNext() const {
if (rev) {
return pos > 0;
} else {
return pos < (m.size_s() - 1);
}
return false;
}
inline bool next() {
if (rev) {
--pos;
return pos >= 0;
} else {
++pos;
return pos < m.size_s();
}
return false;
}
private:
const MapType & m;
ssize_t pos;
bool rev;
};
#ifdef PIP_STD_IOSTREAM #ifdef PIP_STD_IOSTREAM
template<typename Key, typename Type> template<typename Key, typename Type>
inline std::ostream & operator <<(std::ostream & s, const PIMap<Key, Type> & v) { inline std::ostream & operator <<(std::ostream & s, const PIMap<Key, Type> & v) {

View File

@@ -54,12 +54,18 @@ public:
int id; int id;
T data; T data;
}; };
template <typename T>
struct ChunkConst {
ChunkConst(int i, const T & d): id(i), data(d) {}
int id;
const T & data;
};
//! Returns chunk with ID "id" and value "data" for write to stream //! Returns chunk with ID "id" and value "data" for write to stream
template <typename T> static Chunk<T> chunk(int id, const T & data) {return Chunk<T>(id, data);} template <typename T> static ChunkConst<T> chunk(int id, const T & data) {return ChunkConst<T>(id, data);}
//! Add data to this chunk strean with ID "id" and value "data" //! Add data to this chunk strean with ID "id" and value "data"
template <typename T> PIChunkStream & add(int id, const T & data) {*this << Chunk<T>(id, data); return *this;} template <typename T> PIChunkStream & add(int id, const T & data) {*this << ChunkConst<T>(id, data); return *this;}
void setSource(const PIByteArray & data); void setSource(const PIByteArray & data);
void setSource(PIByteArray * data); void setSource(PIByteArray * data);
@@ -112,6 +118,7 @@ private:
PIMap<int, PIByteArray> data_map; PIMap<int, PIByteArray> data_map;
template <typename T> friend PIChunkStream & operator <<(PIChunkStream & s, const PIChunkStream::Chunk<T> & c); template <typename T> friend PIChunkStream & operator <<(PIChunkStream & s, const PIChunkStream::Chunk<T> & c);
template <typename T> friend PIChunkStream & operator <<(PIChunkStream & s, const PIChunkStream::ChunkConst<T> & c);
}; };
template <typename T> template <typename T>
@@ -133,5 +140,24 @@ PIChunkStream & operator <<(PIChunkStream & s, const PIChunkStream::Chunk<T> & c
} }
return s; return s;
} }
template <typename T>
PIChunkStream & operator <<(PIChunkStream & s, const PIChunkStream::ChunkConst<T> & c) {
PIByteArray ba;
ba << c.data;
switch (s.version_) {
case PIChunkStream::Version_1:
(*(s.data_)) << c.id << ba;
break;
case PIChunkStream::Version_2:
if (s.data_->isEmpty())
(*(s.data_)) << uchar(uchar(s.version_) | 0x80);
PIChunkStream::writeVInt(*(s.data_), c.id);
PIChunkStream::writeVInt(*(s.data_), ba.size());
s.data_->append(ba);
break;
default: break;
}
return s;
}
#endif // PICHUNKSTREAM_H #endif // PICHUNKSTREAM_H

View File

@@ -658,6 +658,26 @@ PIString & PIString::replaceAll(const PIString & what, const PIString & with) {
} }
PIString & PIString::replaceAll(const PIString & what, const char with) {
if (what.isEmpty()) return *this;
int l = what.length(), dl = what.length() - 1;
for (int i = 0; i < length() - l + 1; ++i) {
bool match = true;
for (int j = 0; j < l; ++j) {
if (at(j + i) != what[j]) {
match = false;
break;
}
}
if (!match) continue;
if (dl > 0) PIDeque<PIChar>::remove(i, dl);
at(i) = PIChar(with);
//i -= l;
}
return *this;
}
PIString & PIString::replaceAll(const char what, const char with) { PIString & PIString::replaceAll(const char what, const char with) {
int l = length(); int l = length();
for (int i = 0; i < l; ++i) { for (int i = 0; i < l; ++i) {

View File

@@ -283,6 +283,11 @@ public:
* \sa \a replace(), \a replaced() */ * \sa \a replace(), \a replaced() */
PIString & replaceAll(const PIString & what, const PIString & with); PIString & replaceAll(const PIString & what, const PIString & with);
/*! \brief Replace all founded substrings "what" with symbol "with" and return this string
* \details Example: \snippet pistring.cpp PIString::replaceAll
* \sa \a replace(), \a replaced() */
PIString & replaceAll(const PIString & what, const char with);
/*! \brief Replace all founded symbols "what" with symbol "with" and return this string /*! \brief Replace all founded symbols "what" with symbol "with" and return this string
* \details Example: \snippet pistring.cpp PIString::replaceAll * \details Example: \snippet pistring.cpp PIString::replaceAll
* \sa \a replace(), \a replaced() */ * \sa \a replace(), \a replaced() */

View File

@@ -235,7 +235,7 @@ PIByteArray PIFile::readAll(bool forceRead) {
llong s = size(); llong s = size();
if (s < 0) return a; if (s < 0) return a;
a.resize(s); a.resize(s);
s = readAll(a.data()); fread(a.data(), 1, s, PRIVATE->fd);
seek(cp); seek(cp);
if (s >= 0) a.resize(s); if (s >= 0) a.resize(s);
return a; return a;

View File

@@ -34,32 +34,81 @@ int main() {
class db { class db {
public: public:
db() { db() {
name = PIStringAscii("sflner;ljner.vjnrevsg;j35m4;gberjg2mnv");
for (int i=0; i<10000; ++i) for (int i=0; i<10000; ++i)
x << sin(double(i)/180.0); x << sin(double(i)/180.0);
//printf("jkfkhg\n"); //printf("jkfkhg\n");
} }
private: PIString name;
PIVector<double> x; PIVector<double> x;
}; };
inline PIByteArray & operator <<(PIByteArray & ba, const db & v) {
PIChunkStream cs;
cs.add(1, v.name).add(2, v.x);
ba << cs.data();
return ba;
}
inline PIByteArray & operator >>(PIByteArray & ba, db & v) {
PIByteArray src; ba >> src; PIChunkStream cs(src);
while (!cs.atEnd()) {
switch (cs.read()) {
case 1: cs.get(v.name); break;
case 2: cs.get(v.x); break;
}
}
return ba;
}
PIEthernet eth;
#include "picodeparser.h" #include "picodeparser.h"
int main() { int main() {
/*PIString s(" 324 654 sf 5fdwg sdfsdf sdfefg"); //PICodeParser cp;
piCout << s; //cp.parseFile("SH_plugin_base.h");
piCout << s.replaceAll(' ', '1');*/
piDebug = false; PIMap<int, PIString> m;
m[1] = "one";
m[2] = "two";
auto it = m.riterate();
while (it.next()) {
//it.next();
it.valueRef() << "_!";
piCout << it.key() << it.value();
}
/*eth.__meta_data
piForeachC (auto & i, cp.enums) {
i.
}*/
/*piDebug = false;
double min = -1, max = -1, mean = 0; double min = -1, max = -1, mean = 0;
for (int i = 0; i < 1; ++i) { const int iterations = 50;
PICodeParser cp; db d, d2;
for (int i = 0; i < iterations; ++i) {
//PICodeParser cp;
PITimeMeasurer tm; PITimeMeasurer tm;
cp.parseFile("SH_plugin_base.h"); for (int j = 0; j < 100; ++j) {
PIByteArray ba;
ba << d;
}
//cp.parseFile("SH_plugin_base.h");
double ms = tm.elapsed_m(); double ms = tm.elapsed_m();
if (min < 0) min = ms; if (min < 0) min = ms;
if (max < 0) max = ms; if (max < 0) max = ms;
min = piMin(min, ms); min = piMin(min, ms);
max = piMax(max, ms); max = piMax(max, ms);
mean += ms; mean += ms;
PIByteArray ba;
ba << d;
d2.name.clear();
d2.x.clear();
ba >> d2;
} }
piDebug = true; piDebug = true;
piCout << min << (mean / 50) << max; piCout << d2.name << d2.x.size_s();
piCout << min << (mean / iterations) << max;*/
} }