maybe someone has a solution for this already, its a very basic compression technique:
if you have a string like:
a---b-c--de----f-
how can i get one like this:
a3b1c2de4f1
means, replacing the occurrence count of ‘-’ with the string itself.
maybe someone has a solution for this already, its a very basic compression technique:
if you have a string like:
a---b-c--de----f-
how can i get one like this:
a3b1c2de4f1
means, replacing the occurrence count of ‘-’ with the string itself.
ok, now i have this method… anyone wants to optimize?
code(lang=csharp):
//replace method
List FHitList = new List();
protected string CountReplace(string s)
{
//init
FHitList.Clear();
var lastWasHit = false;
SMatch lastHit = null;//gather all hits for (int i=0; i<s.Length; i++) { if (s[i](i) == "-") { if(lastWasHit) { lastHit.count++; } else { lastHit = new SMatch(); lastHit.index = i; lastHit.count = 1; } lastWasHit = true; } else { if (lastWasHit) FHitList.Add(lastHit); lastWasHit = false; } } //add last hit if (lastHit != null) FHitList.Add(lastHit); //substitude the hits for (int i=FHitList.Count-1; i>=0; i--) { //FLogger.Log(LogType.Debug, "hit " + l[i](i).index + " " + l[i](i).count); s = s.Remove(FHitList[i](i).index, FHitList[i](i).count); s = s.Insert(FHitList[i](i).index, FHitList[i](i).count.ToString()); } return s; }
with
code(lang=csharp):
class SMatch
{
public int index;
public int count;
}
ok, with a scan one can build a new string on the fly:
code(lang=csharp):
//replace method
protected string CountReplace(string s, char match)
{
//init
var lastWasHit = false;
var ret = “”;
var hitCount = 0;//scan the string for (int i=0; i<s.Length; i++) { if (s[i](i) == match) { hitCount++; lastWasHit = true; } else { if(lastWasHit) ret += hitCount.ToString(); hitCount = 0; ret += s[i](i); lastWasHit = false; } } //last hit if(lastWasHit) ret += hitCount.ToString(); return ret; }