public int CalculateJsonLength() { // This could possibly change length as a string, thus we need to either recalculate multiple times until there is // no change in length from the last iteration or we need to ensure it takes the same string length no matter the // number. The latter is the best approach as it reduces memory and processor load substantially. // Thus, we need to convert to a string and append 0s to the front to fill the full places of a int. //System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(FullContent.ToString())); //return JsonLength = (int)ms.Length; return(JsonLength = System.Text.Encoding.UTF8.GetBytes(FullContent.ToString()).Length); }
public virtual MultisourcedStream MakeStream(out long contentLength) { MultisourcedStream stream; Timestamp = DateTime.Now; Duration = Timestamp - _start; CalculateJsonLength(); System.IO.MemoryStream msKnot = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(JsonLength.ToString() + "\0")); System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(FullContent.ToString())); if (Stream != null) { stream = new MultisourcedStream(msKnot, ms, Stream); } else { stream = new MultisourcedStream(msKnot, ms); } contentLength = stream.Length; return(stream); }
public void ComputeSegments(string value) { var newSegments = new List <Segment>(); var buf = ""; var isIncorrect = false; int i; for (i = 0; i < Math.Min(FullContent.Length, value.Length); i++) { var correctCh = FullContent[i]; var actualCh = value[i]; if (correctCh == actualCh && isIncorrect) { newSegments.Add(new Segment() { Text = buf, Incorrect = true, Attempted = true, }); buf = ""; isIncorrect = false; } else if (correctCh != actualCh && !isIncorrect) { newSegments.Add(new Segment() { Text = buf, Incorrect = false, Attempted = true, }); buf = ""; isIncorrect = true; } buf += correctCh; } if (!string.IsNullOrEmpty(buf)) { newSegments.Add(new Segment() { Text = buf, Incorrect = isIncorrect, Attempted = true, }); } if (i < FullContent.Length) { newSegments.Add(new Segment() { Text = FullContent.Substring(i), Incorrect = false, Attempted = false, }); } if (i < value.Length) { newSegments.Add(new Segment() { Text = value.Substring(i), Incorrect = true, Attempted = true, }); } Segments = newSegments; }