private void EnsureComparesConsistent(string left, string right) { String8 left8 = left.TestConvert(); String8 right8 = right.TestConvert(); CompareResult caseSensitiveExpected = ToResult(String.Compare(left, right, StringComparison.Ordinal)); CompareResult caseInsensitiveExpected = ToResult(String.Compare(left, right, StringComparison.OrdinalIgnoreCase)); Assert.AreEqual(caseSensitiveExpected, ToResult(left8.CompareTo(right8)), "Case sensitive comparison result incorrect."); Assert.AreEqual(caseInsensitiveExpected, ToResult(left8.CompareTo(right8, true)), "Case insensitive comparison result incorrect."); Assert.AreEqual(caseSensitiveExpected, ToResult(left8.CompareTo(right)), "Case sensitive String8 to string comparison result incorrect."); Assert.AreEqual(caseInsensitiveExpected, ToResult(left8.CompareTo(right, true)), "Case insensitive String8 to string comparison result incorrect."); // StartsWith and CompareAsPrefixTo Assert.AreEqual(left.StartsWith(right), left8.StartsWith(right8)); Assert.AreEqual(left.StartsWith(right, StringComparison.OrdinalIgnoreCase), left8.StartsWith(right8, true)); Assert.AreEqual(right.StartsWith(left), right8.StartsWith(left8)); Assert.AreEqual(right.StartsWith(left, StringComparison.OrdinalIgnoreCase), right8.StartsWith(left8, true)); // Case Insensitive Stable is the insensitive order, then the sensitive order for ties CompareResult caseInsensitiveStableExpected = (caseInsensitiveExpected == CompareResult.Equal ? caseSensitiveExpected : caseInsensitiveExpected); Assert.AreEqual(caseInsensitiveStableExpected, ToResult(left8.CompareCaseInsensitiveStableTo(right8)), "Case insensitive stable String8 to string comparison result incorrect."); }
public void String8_ShiftBack() { String8Block block = new String8Block(); // Goal: Split on semi-colon, collapse semi-colon and spaces in-place String8 shiftable = "One; Two;Three; Four".TestConvert(); int totalShift = 0; String8Set parts = shiftable.Split(UTF8.Semicolon, new PartialArray <int>(5, false)); for (int i = 0; i < parts.Count; ++i) { String8 part = parts[i]; totalShift++; if (part.StartsWith(UTF8.Space)) { part = part.Substring(1); totalShift++; } String8 beforeShift = block.GetCopy(part); String8 shifted = part.ShiftBack(totalShift); Assert.AreEqual(beforeShift, shifted); } String8 result = shiftable.Substring(0, shiftable.Length - totalShift); Assert.AreNotEqual("OneTwoThreeFour", result.ToString()); }
private static void NotStartsWith(string inputFilePath, string outputFilePath, string valueColumnIdentifier, string nameColumnIdentifier) { using (ITabularReader reader = TabularFactory.BuildReader(inputFilePath)) { int valueColumnIndex = reader.ColumnIndex(valueColumnIdentifier); int nameColumnIndex = reader.ColumnIndex(nameColumnIdentifier); using (ITabularWriter writer = TabularFactory.BuildWriter(outputFilePath)) { writer.SetColumns(reader.Columns); while (reader.NextRow()) { String8 name = reader.Current(nameColumnIndex).ToString8(); String8 value = reader.Current(valueColumnIndex).ToString8(); if (!value.StartsWith(name)) { for (int i = 0; i < reader.CurrentRowColumns; ++i) { writer.Write(reader.Current(i).ToString8()); } writer.NextRow(); } } WriteSizeSummary(reader, writer); } } }
public void String8_StartsWithEndsWith() { string collections = "Collections"; String8 collections8 = collections.TestConvert(); string collectionsCasing = "coLLecTionS"; String8 collectionsCasing8 = collectionsCasing.TestConvert(); Assert.IsFalse(String8.Empty.StartsWith(UTF8.Space)); Assert.IsFalse(String8.Empty.EndsWith(UTF8.Space)); Assert.IsTrue(collections8.StartsWith((byte)'C')); Assert.IsFalse(collections8.StartsWith((byte)'c')); Assert.IsFalse(collections8.StartsWith(UTF8.Newline)); Assert.IsTrue(collections8.EndsWith((byte)'s')); Assert.IsFalse(collections8.EndsWith((byte)'S')); Assert.IsFalse(collections8.EndsWith(UTF8.Newline)); Assert.IsFalse(String8.Empty.StartsWith(collections8)); Assert.IsFalse(String8.Empty.EndsWith(collections8)); Assert.IsFalse(String8.Empty.StartsWith(collections8, true)); Assert.IsFalse(String8.Empty.EndsWith(collections8, true)); Assert.IsTrue(collections8.EndsWith(collections8)); Assert.IsTrue(collections8.EndsWith(collections8.Substring(1))); Assert.IsTrue(collections8.EndsWith(collections8.Substring(8))); Assert.IsFalse(collections8.EndsWith(collectionsCasing8)); Assert.IsTrue(collections8.EndsWith(collectionsCasing8, true)); Assert.IsTrue(collections8.StartsWith(collections8)); Assert.IsTrue(collections8.StartsWith(collections8.Substring(0, collections8.Length - 1))); Assert.IsTrue(collections8.StartsWith(collections8.Substring(0, 3))); Assert.IsFalse(collections8.StartsWith(collectionsCasing8)); Assert.IsTrue(collections8.StartsWith(collectionsCasing8, true)); }
public bool NextRow() { _currentRowBlock.Clear(); String8 row = _reader.NextRow(); if (row.IsEmpty()) { return(false); } // Clear values for row for (int i = 0; i < _currentRowValues.Length; ++i) { _currentRowValues[i].SetValue(String8.Empty); } // Read available complete lines String8 currentPropertyName = String8.Empty; String8 currentPropertyValue = String8.Empty; bool currentIsBase64 = false; for (; _nextLineIndex < _blockLines.Count; ++_nextLineIndex) { String8 line = _blockLines[_nextLineIndex]; // Skip comment lines and grouping lines if (line.StartsWith(UTF8.Pound) || line.StartsWith(UTF8.Dash)) { continue; } // Trim trailing CR, if found if (line.EndsWith(UTF8.CR)) { line = line.Substring(0, line.Length - 1); } // An empty line or out of lines for the row range if (line.Length == 0 || line.Index >= row.Index + row.Length) { break; } // Look for a wrapped line if (line[0] == UTF8.Space) { // If found, concatenate the value after the space onto the value so far line = line.Substring(1); currentPropertyValue = _currentRowBlock.Concatenate(currentPropertyValue, String8.Empty, line); } else { // Set or Append the value just completed SetColumnValue(currentPropertyName, currentPropertyValue, currentIsBase64); // Split the property name and value [value is after colon and optional space] currentPropertyName = line.BeforeFirst(UTF8.Colon); currentPropertyValue = line.Substring(currentPropertyName.Length + 1); if (currentPropertyValue.StartsWith(UTF8.Space)) { currentPropertyValue = currentPropertyValue.Substring(1); } // Determine if the value is encoded currentIsBase64 = (line[currentPropertyName.Length + 1] == UTF8.Colon); if (currentIsBase64) { currentPropertyValue = currentPropertyValue.Substring(1); } } } // Set the last property value SetColumnValue(currentPropertyName, currentPropertyValue, currentIsBase64); // The next row starts after the row separator line _nextLineIndex++; this.RowCountRead++; return(true); }