c# - Using StringComparer with StringBuilder to search for a string -


i need use globalization rules search occurrences of string within document. pseudocode is:

var searchtext = "hello, world"; var compareinfo = new cultureinfo("en-us").compareinfo;  documentiterator start = null; // start position if match occurs var sb = new stringbuilder();  // document not string, exposes iterator content (var iter = doc.start(); iter.isvalid(); ++iter) {     start = start ?? iter; // start of potential match      var ch = iter.getchar();      sb.append(ch);      if (compareinfo.compare(searchtext, sb.tostring()) == 0) // exact match     {         console.writeline($"match @ {start}-{iter}");         // not shown: continue search more occurrences.     }     else if (!compareinfo.isprefix(criteria.text, sb.tostring()))     {         // restart search character following start         sb.clear();         iter = start; // gets incremented         start = null;     } } 

this delegates compareinfo difficult job of culture-sensitive string matching.

however, stream-like process implemented code has performance issues because calls stringbuilder.tostring() in every iteration, defeating performance benefit of stringbuilder.

question: how can search efficiently?

so why not copy whole document stringbuilder first, use 1 tostring(). use similar scheme iterate on possible values. use compareinfo.compare(criteria.text, 0, criteria.text.length, docstring, startindex, checklength)


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -