public void EquivalentSubstringsAreEqual() { const string s1 = "Hello"; const string s2 = "Hello"; var ss1 = new Substring(s1, 2); var ss2 = new Substring(s2, 2); ss1.Equals(ss2).Should().BeTrue(); ss1.Equals(ss2 as object).Should().BeTrue(); (ss1 == ss2).Should().BeTrue(); (ss1 != ss2).Should().BeFalse(); }
public void DifferentSubstringsAreNotEqual() { var ss1 = new Substring("Hello", 2, 1); ss1.ToString().Should().Be("l"); var ss2 = new Substring("Hello", 3, 1); ss2.ToString().Should().Be("l"); ss1.Equals(ss2).Should().BeFalse(); ss1.Equals(ss2 as object).Should().BeFalse(); (ss1 == ss2).Should().BeFalse(); (ss1 != ss2).Should().BeTrue(); }
public void SubstringDoesNotEqualEquivalentString() { const string s = "Hello"; var ss = new Substring(s, 0); ss.Equals(s).Should().BeFalse(); }
/** ************************************************************************************ * Utility method that checks if a given value represents 'true'. * @param value The value to check * * @return Returns \c true, if the value represents 'true', \c false if not. **************************************************************************************/ public bool IsTrue(Substring value) { foreach (AString s in TrueValues) { if (value.Equals(s, Case.Ignore)) { return(true); } } return(false); }
// ############################################################################################# // Internals // ############################################################################################# /** **************************************************************************************** * Internal, recursive helper of #Find. * * @param domainPath Path to search. * @param sensitivity Denotes if domain name search is treated case sensitive or not. * @param maxCreate The maximum number of sub domains that are created if not * found at the end of the path. * @param[out] wasCreated Output parameter that is set \c true if domain was not found * and hence created. * @return The domain found or created. ******************************************************************************************/ protected Domain findRecursive(Substring domainPath, Case sensitivity, int maxCreate, ref bool wasCreated) { //--- get act sub-name and rest of path domainPath.Consume(PathSeparator); int endSubName = domainPath.IndexOf(PathSeparator); ALIB.ASSERT_ERROR(endSubName != 0, "Internal Error"); // find end of actual domain name and save rest Substring restOfDomainPath = tSubstring2; restOfDomainPath.SetNull(); if (endSubName > 0) { domainPath.Split(endSubName, restOfDomainPath, 1); } // search sub-domain Domain subDomain = null; // "." if (domainPath.Equals(".")) { subDomain = this; } // ".." else if (domainPath.Equals("..")) { subDomain = Parent != null ? Parent : this; } // search in sub-domain else { int i; bool fixedOnce = false; for (;;) { for (i = 0; i < SubDomains.Count; i++) { int comparison = SubDomains[i].Name.CompareTo(domainPath, sensitivity); if (comparison >= 0) { if (comparison == 0) { subDomain = SubDomains[i]; } break; } } // domain found? if (subDomain != null) { break; } // try and fix name if (!fixedOnce) { fixedOnce = true; bool illegalCharacterFound = false; for (int cp = 0; cp < domainPath.Length(); ++cp) { char c = domainPath.CharAt(cp); if (c < '-' || c > 'z' || c == '<' || c == '>' || c == '[' || c == ']' || c == '=' || c == '?' || c == ';' || c == ':' || c == '\\' || c == '\'' || c == '.' || c == ',' ) { illegalCharacterFound = true; domainPath.Buf[domainPath.Start + cp] = '#'; } } if (illegalCharacterFound) { continue; } } // create if (maxCreate == 0) { return(null); } wasCreated = true; SubDomains.Insert(i, subDomain = new Domain(this, new AString(domainPath))); maxCreate--; if (maxCreate == 0) { return(subDomain); } break; } } // recursion? if (restOfDomainPath.IsNotEmpty()) { domainPath.Set(restOfDomainPath); return(subDomain.findRecursive(domainPath, sensitivity, maxCreate, ref wasCreated)); } // that's it return(subDomain); }
public void CompareTo() { Substring tss = new Substring(); // null string comparison String nullString = null; UT_TRUE(tss.CompareTo(nullString) == 0); UT_TRUE(tss.Equals(nullString)); tss.Set(""); UT_TRUE(tss.CompareTo(nullString) != 0); UT_TRUE(!tss.Equals(nullString)); tss.SetNull(); UT_TRUE(tss.CompareTo(nullString) == 0); UT_TRUE(tss.Equals(nullString)); String t = "abcde"; tss = new Substring("01" + t + "234", 2, t.Length); int result, resSys; result = tss.CompareTo(t); UT_EQ(t.CompareTo(t), result); result = tss.CompareTo(t + "x"); UT_EQ(t.CompareTo(t + "x"), result); result = tss.CompareTo(t.Substring(0, t.Length - 1)); UT_EQ(t.CompareTo(t.Substring(0, t.Length - 1)), result); result = tss.CompareTo("pad" + t, Case.Sensitive, 3); UT_EQ(0, result); result = tss.CompareTo("pad" + t, Case.Sensitive, 3, 2); UT_EQ(1, result); // greater/smaller strings { String greater = "x"; String greater2 = "abcdef"; String smaller = "aaa"; String smaller2 = "abcd"; result = tss.CompareTo(greater); resSys = t.CompareTo(greater); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0)); result = tss.CompareTo(greater2); resSys = t.CompareTo(greater2); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0)); result = tss.CompareTo(smaller); resSys = t.CompareTo(smaller); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0)); result = tss.CompareTo(smaller2); resSys = t.CompareTo(smaller2); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0)); String ut = t.ToUpper(); UT_FALSE(0 == tss.CompareTo(ut)); UT_FALSE(0 == tss.CompareTo(ut, Case.Sensitive)); UT_TRUE(0 == tss.CompareTo(ut, Case.Ignore)); } { AString greater = new AString("x"); AString greater2 = new AString("abcdef"); AString smaller = new AString("aaa"); AString smaller2 = new AString("abcd"); result = tss.CompareTo(greater); resSys = t.CompareTo(greater.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0)); result = tss.CompareTo(greater2); resSys = t.CompareTo(greater2.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0)); result = tss.CompareTo(smaller); resSys = t.CompareTo(smaller.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0)); result = tss.CompareTo(smaller2); resSys = t.CompareTo(smaller2.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0)); AString ut = new AString(t.ToUpper()); UT_FALSE(0 == tss.CompareTo(ut)); UT_FALSE(0 == tss.CompareTo(ut, Case.Sensitive)); UT_TRUE(0 == tss.CompareTo(ut, Case.Ignore)); } { Substring greater = new Substring("123x", 3); Substring greater2 = new Substring("123abcdef", 3); Substring smaller = new Substring("123aaa", 3); Substring smaller2 = new Substring("123abcd", 3); result = tss.CompareTo(greater); resSys = t.CompareTo(greater.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0)); result = tss.CompareTo(greater2); resSys = t.CompareTo(greater2.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0)); result = tss.CompareTo(smaller); resSys = t.CompareTo(smaller.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0)); result = tss.CompareTo(smaller2); resSys = t.CompareTo(smaller2.ToString()); UT_TRUE(result == resSys || (result < 0 && resSys < 0) || (result > 0 && resSys > 0)); Substring ut = new Substring(t.ToUpper()); UT_FALSE(0 == tss.CompareTo(ut)); UT_FALSE(0 == tss.CompareTo(ut, Case.Sensitive)); UT_TRUE(0 == tss.CompareTo(ut, Case.Ignore)); } }
public void Consume() { // null substring { Substring s = new Substring(); Substring r = new Substring("oldval"); UT_EQ('\0', s.Consume( )); UT_EQ(0, s.Consume(0, r)); UT_TRUE(r.IsNull()); UT_EQ(0, s.Consume(5, r)); UT_TRUE(r.IsNull()); UT_EQ(false, s.Consume('a')); UT_EQ(false, s.Consume("word")); UT_EQ('\0', s.ConsumeFromEnd( )); UT_EQ(0, s.ConsumeFromEnd(0)); UT_EQ(0, s.ConsumeFromEnd(5)); UT_EQ(false, s.ConsumeFromEnd('a')); UT_EQ(false, s.ConsumeFromEnd("word")); } // empty substring { Substring s = new Substring("aaaaaaaaaaaa"); Substring r = new Substring("oldval"); s.Start = 5; s.End = 4; UT_EQ('\0', s.Consume( )); UT_EQ(0, s.Consume(0, r)); UT_TRUE(r.IsNotNull()); UT_TRUE(r.IsEmpty()); UT_EQ(0, s.Consume(5, r)); UT_TRUE(r.IsNotNull()); UT_TRUE(r.IsEmpty()); UT_EQ(false, s.Consume('a')); UT_EQ(false, s.Consume("word")); UT_EQ('\0', s.ConsumeFromEnd( )); UT_EQ(0, s.ConsumeFromEnd(0)); UT_EQ(0, s.ConsumeFromEnd(5)); UT_EQ(false, s.ConsumeFromEnd('a')); UT_EQ(false, s.ConsumeFromEnd("word")); } // substring of length 1 { Substring s = new Substring("aaaaaaaaaaaa"); Substring r = new Substring("oldval"); s.Start = s.End = 5; UT_EQ('a', s.Consume( )); UT_EQ(0, s.Length()); s.Start = s.End = 5; UT_EQ(1, s.Consume(0)); UT_EQ(1, s.Length()); s.Start = s.End = 5; UT_EQ(0, s.Consume(1, r)); UT_EQ(0, s.Length()); UT_TRUE(r.Equals("a")); s.Start = s.End = 5; UT_EQ(0, s.Consume(5, r)); UT_EQ(0, s.Length()); UT_TRUE(r.Equals("a")); s.Start = s.End = 5; UT_EQ(true, s.Consume('a')); UT_EQ(0, s.Length()); s.Start = s.End = 5; UT_EQ(false, s.Consume('b')); UT_EQ(1, s.Length()); s.Start = s.End = 5; UT_EQ(false, s.Consume("word")); UT_EQ(1, s.Length()); s.Start = s.End = 5; UT_EQ('a', s.ConsumeFromEnd( )); UT_EQ(0, s.Length()); s.Start = s.End = 5; UT_EQ(1, s.ConsumeFromEnd(0)); UT_EQ(1, s.Length()); s.Start = s.End = 5; UT_EQ(0, s.ConsumeFromEnd(1)); UT_EQ(0, s.Length()); s.Start = s.End = 5; UT_EQ(0, s.ConsumeFromEnd(5)); UT_EQ(0, s.Length()); s.Start = s.End = 5; UT_EQ(true, s.ConsumeFromEnd('a')); UT_EQ(0, s.Length()); s.Start = s.End = 5; UT_EQ(false, s.ConsumeFromEnd('b')); UT_EQ(1, s.Length()); s.Start = s.End = 5; UT_EQ(false, s.ConsumeFromEnd("word")); UT_EQ(1, s.Length()); } // substring of length 2 { Substring s = new Substring("12ab3456"); Substring r = new Substring("oldval"); s.Start = 2; s.End = 3; UT_EQ('a', s.Consume( )); UT_EQ(1, s.Length()); UT_EQ('b', s.Consume( )); UT_EQ(0, s.Length()); s.Start = 2; s.End = 3; UT_EQ('b', s.ConsumeFromEnd( )); UT_EQ(1, s.Length()); UT_EQ('a', s.ConsumeFromEnd( )); UT_EQ(0, s.Length()); s.Start = 2; s.End = 3; UT_EQ(2, s.Consume(0, r)); UT_EQ(2, s.Length()); UT_TRUE(r.IsNotNull()); UT_TRUE(r.IsEmpty()); s.Start = 2; s.End = 3; UT_EQ(1, s.Consume(1, r)); UT_EQ(1, s.Length()); UT_TRUE(r.Equals("a")); s.Start = 2; s.End = 3; UT_EQ(0, s.Consume(2, r)); UT_EQ(0, s.Length()); UT_TRUE(r.Equals("ab")); s.Start = 2; s.End = 3; UT_EQ(0, s.Consume(3, r)); UT_EQ(0, s.Length()); UT_TRUE(r.Equals("ab")); s.Start = 2; s.End = 3; UT_EQ(2, s.ConsumeFromEnd(0, r)); UT_EQ(2, s.Length()); UT_TRUE(r.IsNotNull()); UT_TRUE(r.IsEmpty()); s.Start = 2; s.End = 3; UT_EQ(1, s.ConsumeFromEnd(1, r)); UT_EQ(1, s.Length()); UT_TRUE(r.Equals("b")); s.Start = 2; s.End = 3; UT_EQ(0, s.ConsumeFromEnd(2, r)); UT_EQ(0, s.Length()); UT_TRUE(r.Equals("ab")); s.Start = 2; s.End = 3; UT_EQ(0, s.ConsumeFromEnd(3, r)); UT_EQ(0, s.Length()); UT_TRUE(r.Equals("ab")); s.Start = 2; s.End = 3; UT_EQ(false, s.Consume('b')); UT_EQ(2, s.Length()); UT_EQ(true, s.Consume('a')); UT_EQ(1, s.Length()); UT_EQ(true, s.Consume('b')); UT_EQ(0, s.Length()); UT_EQ(false, s.Consume('a')); UT_EQ(0, s.Length()); UT_EQ(false, s.Consume('b')); UT_EQ(0, s.Length()); s.Start = 2; s.End = 3; UT_EQ(false, s.ConsumeFromEnd('a')); UT_EQ(2, s.Length()); UT_EQ(true, s.ConsumeFromEnd('b')); UT_EQ(1, s.Length()); UT_EQ(true, s.ConsumeFromEnd('a')); UT_EQ(0, s.Length()); UT_EQ(false, s.ConsumeFromEnd('b')); UT_EQ(0, s.Length()); UT_EQ(false, s.ConsumeFromEnd('a')); UT_EQ(0, s.Length()); s.Start = 2; s.End = 3; UT_EQ(false, s.Consume("word")); UT_EQ(2, s.Length()); s.Start = 2; s.End = 3; UT_EQ(false, s.Consume("AB")); UT_EQ(2, s.Length()); s.Start = 2; s.End = 3; UT_EQ(true, s.Consume("ab")); UT_EQ(0, s.Length()); s.Start = 2; s.End = 3; UT_EQ(false, s.ConsumeFromEnd("word")); UT_EQ(2, s.Length()); s.Start = 2; s.End = 3; UT_EQ(false, s.ConsumeFromEnd("AB")); UT_EQ(2, s.Length()); s.Start = 2; s.End = 3; UT_EQ(true, s.ConsumeFromEnd("ab")); UT_EQ(0, s.Length()); } // 3 words { Substring s = new Substring("word1 word2 word3"); UT_EQ('w', s.Consume( )); UT_EQ('o', s.Consume( )); UT_EQ('r', s.Consume( )); UT_EQ('d', s.Consume( )); UT_EQ('1', s.Consume( )); UT_EQ(false, s.Consume('w')); UT_EQ(true, s.Consume('w', Whitespaces.Trim)); UT_EQ(true, s.Consume('o', Whitespaces.Trim)); UT_EQ(false, s.Consume('o', Whitespaces.Trim)); UT_EQ(true, s.Consume('r', Whitespaces.Trim)); UT_EQ(false, s.Consume("D2", Whitespaces.Trim)); UT_EQ(false, s.Consume("D2")); UT_EQ(true, s.Consume("d2")); UT_EQ(2, s.Consume(4)); UT_EQ("d3", s.ToString()); s = new Substring("word1 word2 word3"); UT_EQ('3', s.ConsumeFromEnd( )); UT_EQ('d', s.ConsumeFromEnd( )); UT_EQ('r', s.ConsumeFromEnd( )); UT_EQ('o', s.ConsumeFromEnd( )); UT_EQ('w', s.ConsumeFromEnd( )); UT_EQ(false, s.ConsumeFromEnd('2')); UT_EQ(true, s.ConsumeFromEnd('2', Whitespaces.Trim)); UT_EQ(true, s.ConsumeFromEnd('d', Whitespaces.Trim)); UT_EQ(false, s.ConsumeFromEnd('d', Whitespaces.Trim)); UT_EQ(true, s.ConsumeFromEnd('r', Whitespaces.Trim)); UT_EQ(false, s.ConsumeFromEnd("WO", Whitespaces.Trim)); UT_EQ(false, s.ConsumeFromEnd("WO")); UT_EQ(true, s.ConsumeFromEnd("wo")); UT_EQ(2, s.ConsumeFromEnd(4)); UT_EQ("wo", s.ToString()); } // consume AString, Substring { Substring s = new Substring("word1 word2 word3 word4"); Substring sConsume = new Substring("1234word12", 4, 4); AString aConsume = new AString("word"); UT_EQ(true, s.Consume(sConsume)); UT_EQ(false, s.Consume(sConsume)); UT_EQ('1', s.Consume( )); UT_EQ(false, s.Consume(sConsume)); UT_EQ(true, s.Consume(sConsume, Whitespaces.Trim)); UT_EQ('2', s.Consume( )); UT_EQ(' ', s.Consume( )); UT_EQ(true, s.Consume(aConsume)); UT_EQ(false, s.Consume(aConsume)); UT_EQ('3', s.Consume( )); UT_EQ(false, s.Consume(aConsume)); UT_EQ(true, s.Consume(aConsume, Whitespaces.Trim)); s.Set("1word 2word 3word 4word"); UT_EQ(true, s.ConsumeFromEnd(sConsume)); UT_EQ(false, s.ConsumeFromEnd(sConsume)); UT_EQ('4', s.ConsumeFromEnd( )); UT_EQ(false, s.ConsumeFromEnd(sConsume)); UT_EQ(true, s.ConsumeFromEnd(sConsume, Whitespaces.Trim)); UT_EQ('3', s.ConsumeFromEnd( )); UT_EQ(' ', s.ConsumeFromEnd( )); UT_EQ(true, s.ConsumeFromEnd(aConsume)); UT_EQ(false, s.ConsumeFromEnd(aConsume)); UT_EQ('2', s.ConsumeFromEnd( )); UT_EQ(false, s.ConsumeFromEnd(aConsume)); UT_EQ(true, s.ConsumeFromEnd(aConsume, Whitespaces.Trim)); } }
void tEquals(String haystack, String needle, bool expectedSensitive, bool expectedIgnoreCase) { haystackAString._()._(haystack); haystackSubstring.Set("01" + haystack + "234", 2, haystack.Length); if (needle != null) { UT_EQ(expectedSensitive, haystackAString.Equals(needle, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackAString.Equals(needle, Case.Ignore)); UT_EQ(expectedSensitive, haystackSubstring.Equals(needle, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackSubstring.Equals(needle, Case.Ignore)); needleAString._()._(needle); UT_EQ(expectedSensitive, haystackAString.Equals(needleAString, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackAString.Equals(needleAString, Case.Ignore)); UT_EQ(expectedSensitive, haystackSubstring.Equals(needleAString, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackSubstring.Equals(needleAString, Case.Ignore)); needleSubstring.Set("XY" + needle + "Z", 2, needle.Length).Trim(); UT_EQ(expectedSensitive, haystackAString.Equals(needleSubstring, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackAString.Equals(needleSubstring, Case.Ignore)); UT_EQ(expectedSensitive, haystackSubstring.Equals(needleSubstring, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackSubstring.Equals(needleSubstring, Case.Ignore)); } else { UT_EQ(expectedSensitive, haystackAString.Equals(needle, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackAString.Equals(needle, Case.Ignore)); UT_EQ(expectedSensitive, haystackAString.Equals((AString)null, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackAString.Equals((AString)null, Case.Ignore)); UT_EQ(expectedSensitive, haystackAString.Equals((Substring)null, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackAString.Equals((Substring)null, Case.Ignore)); UT_EQ(expectedSensitive, haystackSubstring.Equals(needle, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackSubstring.Equals(needle, Case.Ignore)); UT_EQ(expectedSensitive, haystackSubstring.Equals((AString)null, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackSubstring.Equals((AString)null, Case.Ignore)); UT_EQ(expectedSensitive, haystackSubstring.Equals((Substring)null, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackSubstring.Equals((Substring)null, Case.Ignore)); } }