void tIndexOfChar(String haystack, char needle, int startPos, int expectedResult) { haystackAString._()._(haystack); haystackSubsPlain.Set(haystack); haystackSubstring.Set("01" + haystack + haystack + "234", 2 + haystack.Length, haystack.Length); UT_EQ(expectedResult, haystackAString.IndexOf(needle, startPos)); UT_EQ(expectedResult, haystackSubsPlain.IndexOf(needle, startPos)); UT_EQ(expectedResult, haystackSubstring.IndexOf(needle, startPos)); }
/** ******************************************************************************************** * Searches a domain. If not found, the domain is (or path of domains are) created in * the domain tree. * If the path string starts with the character defined in #PathSeparator, then * the search (and creation) is done starting from the root domain of this domain and not * from this domain. * * @param domainPathAS Path and domain 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. **********************************************************************************************/ public Domain Find(AString domainPathAS, Case sensitivity, int maxCreate, ref bool wasCreated) { Substring domainPath = tSubstring; domainPath.Set(domainPathAS); // set optional output parameter as default to false wasCreated = false; int lenBeforeTrim = domainPath.Length(); // if string is empty (resp. contains only separator characters), return ourselves while (domainPath.Consume(PathSeparator)) { ; } if (domainPath.IsEmpty()) { return(this); } // Trailing domain separator found: call find on root domain Domain startDomain = this; if (lenBeforeTrim > domainPath.Length()) { while (startDomain.Parent != null) { startDomain = startDomain.Parent; } } // call find return(startDomain.findRecursive(domainPath, sensitivity, maxCreate, ref wasCreated)); }
// ############################################################################################# // Internals // ############################################################################################# /** **************************************************************************************** * Retrieves and optionally creates an entry in the map that stores language-related * scope information. The result is stored in field #actPathMapNode. * @param create If \c true, a non-existing entry is created. ******************************************************************************************/ protected void getPathMapNode(bool create) { lazyLanguageNode = false; // key: Path languageKey._()._(scopeInfo.GetTrimmedPath()); if (actScope == Scope.Path) { // substract folders at the back int pathLevel = actPathLevel; while (pathLevel > 0) { int idx = languageKey.LastIndexOf(Path.DirectorySeparatorChar); if (idx < 0) { languageKey._(); break; } languageKey.SetLength_NC(idx); pathLevel--; } languageKey._(separators[1]); actPathMapNode = languageStore.Get(languageKeySubstr.Set(languageKey), create, separators); return; } else { languageKey._(separators[1]); } // key: filename languageKey._(scopeInfo.GetFileName()) ._(separators[0]); // key: method if (actScope == Scope.Method) { languageKey._(scopeInfo.GetMethod()) ._(separators[0]); } actPathMapNode = languageStore.Get(languageKeySubstr.Set(languageKey), create, separators); }
public void SubstringConstructor() { AString astr = new AString(); AString res = new AString(); constructorTest("a", res, false); UT_EQ("a", res); constructorTest(" a", res, false); UT_EQ(" a", res); constructorTest("a ", res, false); UT_EQ("a ", res); constructorTest("a b", res, false); UT_EQ("a b", res); constructorTest(" a b", res, false); UT_EQ(" a b", res); constructorTest("a b ", res, false); UT_EQ("a b ", res); constructorTest("a", res, true); UT_EQ("a", res); constructorTest(" a", res, true); UT_EQ("a", res); constructorTest("a ", res, true); UT_EQ("a", res); constructorTest("a b", res, true); UT_EQ("a b", res); constructorTest(" a b", res, true); UT_EQ("a b", res); constructorTest("a b ", res, true); UT_EQ("a b", res); // changing whitespaces { { astr.Clear()._("xy xz abc xy"); Substring subs = new Substring(); subs.Set(astr).Trim("xy ".ToCharArray()); subs.CopyTo(res); UT_EQ("z abc", res); } { Substring subs = new Substring("xy xz abc xy"); subs.TrimStart("xy ".ToCharArray()); subs.TrimEnd("xy ".ToCharArray()); subs.CopyTo(res); UT_EQ("z abc", res); } } // test other constructors { astr.Clear()._(" astring "); UT_TRUE((new Substring()).IsEmpty()); UT_TRUE((new Substring()).IsNull()); UT_EQ("astring", (new Substring(astr)).Trim().ToString()); UT_EQ("str", (new Substring(astr, 2, 3)).ToString()); UT_EQ("", (new Substring(astr, 20, 3)).ToString()); UT_TRUE((new Substring(astr, 20, 3)).IsEmpty()); UT_FALSE((new Substring(astr, 20, 3)).IsNull()); Substring s2 = new Substring(astr); s2.Trim(); UT_EQ("astring", new Substring(s2.ToString()).ToString()); UT_EQ("str", (new Substring((new Substring(astr, 2, 3)))).ToString()); } }
/** **************************************************************************************** * Returns the currently remaining string (without searching for further delimiter * characters).<br> * After this call #HasNext will return false and #Next will return a nulled Substring. * @param trimming Determines if the token is trimmed in respect to the white space * characters defined in field #Whitespaces. * Defaults to \c Whitespaces.Trim. * @return The rest of the original source string, which was not returned by #Next(), yet. ******************************************************************************************/ public Substring GetRest(Whitespaces trimming = enums.Whitespaces.Trim) { // set start, end and end of tokenizer Actual.Set(Rest); if (trimming == enums.Whitespaces.Trim) { Actual.Trim(Whitespaces); } Rest.SetNull(); return(Actual); }
void tStartsWith(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.StartsWith(needle, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackAString.StartsWith(needle, Case.Ignore)); UT_EQ(expectedSensitive, haystackSubstring.StartsWith(needle, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackSubstring.StartsWith(needle, Case.Ignore)); needleAString._()._(needle); UT_EQ(expectedSensitive, haystackAString.StartsWith(needleAString, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackAString.StartsWith(needleAString, Case.Ignore)); UT_EQ(expectedSensitive, haystackSubstring.StartsWith(needleAString, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackSubstring.StartsWith(needleAString, Case.Ignore)); needleSubstring.Set("XY" + needle + "Z", 2, needle.Length).Trim(); UT_EQ(expectedSensitive, haystackAString.StartsWith(needleSubstring, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackAString.StartsWith(needleSubstring, Case.Ignore)); UT_EQ(expectedSensitive, haystackSubstring.StartsWith(needleSubstring, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackSubstring.StartsWith(needleSubstring, Case.Ignore)); } else { UT_EQ(expectedSensitive, haystackAString.StartsWith(needle, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackAString.StartsWith(needle, Case.Ignore)); UT_EQ(expectedSensitive, haystackAString.StartsWith((AString)null, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackAString.StartsWith((AString)null, Case.Ignore)); UT_EQ(expectedSensitive, haystackAString.StartsWith((Substring)null, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackAString.StartsWith((Substring)null, Case.Ignore)); UT_EQ(expectedSensitive, haystackSubstring.StartsWith(needle, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackSubstring.StartsWith(needle, Case.Ignore)); UT_EQ(expectedSensitive, haystackSubstring.StartsWith((AString)null, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackSubstring.StartsWith((AString)null, Case.Ignore)); UT_EQ(expectedSensitive, haystackSubstring.StartsWith((Substring)null, Case.Sensitive)); UT_EQ(expectedIgnoreCase, haystackSubstring.StartsWith((Substring)null, Case.Ignore)); } }
bool Get(Object category, Object name, AString target) { // assemble option name as CATEGORY_NAME target.Clear()._(category); if (target.IsNotEmpty()) { target._('_'); } target._(name); int optionLength = target.Length(); Substring actVar = new Substring(); for (int i = 0; i < args.Length; i++) { // remove whitespaces (if somebody would work with quotation marks...) actVar.Set(args[i]).Trim(); // request '-' and allow a second '-' if (!actVar.Consume('-')) { continue; } actVar.Consume('-'); if (target.CompareTo(args[i], Case.Ignore, actVar.Start, optionLength, 0, optionLength) == 0) { //again, lets trim before searching the = sign (really almost unnecessary) actVar.Start += optionLength; if (actVar.Consume('=', Whitespaces.Trim)) { actVar.TrimStart(); actVar.TrimEnd(); target.Clear(); target._NC(args[i], actVar.Start, actVar.Length()); return(true); } } } return(false); }
public void ParseNumbers() { // ConsumeInteger() { Substring subs = new Substring(); int result; UT_EQ(false, subs.ConsumeInteger(out result)); UT_EQ(0, result); subs.Set(""); UT_EQ(false, subs.ConsumeInteger(out result)); UT_EQ(0, result); subs.Set(" ABC"); UT_EQ(false, subs.ConsumeInteger(out result)); UT_EQ(0, result); subs.Set(" 12345"); UT_EQ(true, subs.ConsumeInteger(out result)); UT_EQ(12345, result); subs.Set(" 12 45"); UT_EQ(true, subs.ConsumeInteger(out result)); UT_EQ(12, result); UT_EQ(true, subs.ConsumeInteger(out result)); UT_EQ(45, result); subs.Set(" 42 ; 7 ; 6 "); UT_EQ(true, subs.ConsumeInteger(out result)); UT_EQ(42, result); UT_EQ(false, subs.ConsumeInteger(out result)); UT_EQ(0, result); UT_EQ(false, subs.ConsumeInteger(out result)); UT_EQ(0, result); char[] ws = " ;".ToCharArray(); subs.Set(" 42 ; 7 ; 6 "); UT_EQ(true, subs.ConsumeInteger(out result, ws)); UT_EQ(42, result); UT_EQ(true, subs.ConsumeInteger(out result, ws)); UT_EQ(7, result); UT_EQ(true, subs.ConsumeInteger(out result, ws)); UT_EQ(6, result); UT_EQ(false, subs.ConsumeInteger(out result, ws)); UT_EQ(0, result); UT_EQ(false, subs.ConsumeInteger(out result, ws)); UT_EQ(0, result); } // ConsumeFloat() { Substring subs = new Substring(); double result; UT_EQ(false, subs.ConsumeFloat(out result)); UT_EQ(0.0, result); subs.Set(""); UT_EQ(false, subs.ConsumeFloat(out result)); UT_EQ(0.0, result); subs.Set(" ABC"); UT_EQ(false, subs.ConsumeFloat(out result)); UT_EQ(0.0, result); subs.Set(" 12345"); UT_EQ(true, subs.ConsumeFloat(out result)); UT_EQ(12345.0, result); subs.Set(" 12.45 "); UT_EQ(true, subs.ConsumeFloat(out result)); UT_EQ(12.45, result); subs.Set(" 12 45"); UT_EQ(true, subs.ConsumeFloat(out result)); UT_EQ(12.0, result); UT_EQ(true, subs.ConsumeFloat(out result)); UT_EQ(45.0, result); char[] ws = " ;".ToCharArray(); subs.Set(" 42.3 ; 0.7 ; 6 "); UT_EQ(true, subs.ConsumeFloat(out result, null, ws)); UT_EQ(42.3, result); UT_EQ(true, subs.ConsumeFloat(out result, null, ws)); UT_EQ(0.7, result); UT_EQ(true, subs.ConsumeFloat(out result, null, ws)); UT_EQ(6.0, result); UT_EQ(false, subs.ConsumeFloat(out result, null, ws)); UT_EQ(0.0, result); UT_EQ(false, subs.ConsumeFloat(out result, null, ws)); UT_EQ(0.0, result); } }
// ############################################################################################# // Interface // ############################################################################################# /** **************************************************************************************** * Sets the tokenizer to the new source and delim. * * @param str The character array to use as the source for the tokens. * @param delim The delimiter that separates the tokens. Can be changed with every * next token. ******************************************************************************************/ public void Set(String str, char delim) { this.delim = delim; Rest.Set(str); Actual.SetNull(); }
// ############################################################################################# // 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 Trim() { Substring subs = new Substring(); { String t; t = ""; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ("", subs.ToString()); t = " "; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ("", subs.ToString()); t = " "; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ("", subs.ToString()); t = "abc"; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ("abc", subs.ToString()); t = "abc"; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ("abc", subs.ToString()); t = " abc"; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ("abc", subs.ToString()); t = " abc"; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ("abc", subs.ToString()); t = " \tabc"; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ("abc", subs.ToString()); t = "\t abc"; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ("abc", subs.ToString()); t = "abc\r\n"; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ("abc", subs.ToString()); t = "\tabc\r\n"; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ("abc", subs.ToString()); t = "\tabc\rx\n"; subs.Set(" pad " + t + " abc ", 5, t.Length).Trim(); UT_EQ("abc\rx", subs.ToString()); } }
public void IndexOf() { Substring subs; // indexOf() { subs = new Substring("ABCD"); UT_EQ(-1, subs.IndexOf('X')); UT_EQ(0, subs.IndexOf('A')); UT_EQ(1, subs.IndexOf('B')); UT_EQ(2, subs.IndexOf('C')); UT_EQ(3, subs.IndexOf('D')); } // search characters subs.Set("abc@" + "abcd abcd" + "abc@de", 4, 9); { UT_EQ(-1, subs.IndexOf('@', -5)); UT_EQ(-1, subs.IndexOf('@')); UT_EQ(-1, subs.IndexOf('@', 5)); UT_EQ(-1, subs.IndexOf('@', 150)); UT_EQ(0, subs.IndexOf('a')); UT_EQ(1, subs.IndexOf('b')); UT_EQ(2, subs.IndexOf('c')); UT_EQ(0, subs.IndexOf('a', 0)); UT_EQ(1, subs.IndexOf('b', 0)); UT_EQ(2, subs.IndexOf('c', 0)); UT_EQ(5, subs.IndexOf('a', 1)); UT_EQ(1, subs.IndexOf('b', 1)); UT_EQ(2, subs.IndexOf('c', 1)); UT_EQ(5, subs.IndexOf('a', 2)); UT_EQ(6, subs.IndexOf('b', 2)); UT_EQ(2, subs.IndexOf('c', 2)); UT_EQ(5, subs.IndexOf('a', 3)); UT_EQ(6, subs.IndexOf('b', 3)); UT_EQ(7, subs.IndexOf('c', 3)); UT_EQ(8, subs.IndexOf('d', 7)); UT_EQ(8, subs.IndexOf('d', 8)); UT_EQ(-1, subs.IndexOf('d', 9)); } // search null, empty string subs.Set("abc@" + "abcd abcd" + "abc@de", 4, 9); { UT_EQ(0, subs.IndexOf((AString)null)); UT_EQ(5, subs.IndexOf((AString)null, 5)); UT_EQ(-1, subs.IndexOf((AString)null, 50)); UT_EQ(0, subs.IndexOf((AString)null, -5)); UT_EQ(0, subs.IndexOf((String)null)); UT_EQ(5, subs.IndexOf((String)null, 5)); UT_EQ(-1, subs.IndexOf((String)null, 50)); UT_EQ(0, subs.IndexOf((String)null, -5)); UT_EQ(0, subs.IndexOf("", -5)); UT_EQ(0, subs.IndexOf("", 0)); UT_EQ(4, subs.IndexOf("", 4)); UT_EQ(-1, subs.IndexOf("", 100)); } // search subs.Set("abc@" + "abcd abcd" + "abc@de", 4, 9); { UT_EQ(0, subs.IndexOf("abcd")); UT_EQ(1, subs.IndexOf("b")); UT_EQ(4, subs.IndexOf(" abcd")); UT_EQ(5, subs.IndexOf("abcd", 1)); UT_EQ(0, subs.IndexOf("abcd", -1)); UT_EQ(-1, subs.IndexOf("xyz", -10)); } // ignore case String t = "Hallo A-Worx util"; subs.Set("abc@" + t + "abc@de", 4, t.Length); { UT_EQ(6, subs.IndexOf("a-worx", 0, Case.Ignore)); UT_EQ(6, subs.IndexOf("a-worx", 1, Case.Ignore)); UT_EQ(6, subs.IndexOf("a-worx", -10, Case.Ignore)); UT_EQ(6, subs.IndexOf("a-worx", 6, Case.Ignore)); UT_EQ(-1, subs.IndexOf("a-worx", 7, Case.Ignore)); UT_EQ(-1, subs.IndexOf("a-worx", 100, Case.Ignore)); UT_EQ(0, subs.IndexOf("hal", 0, Case.Ignore)); UT_EQ(-1, subs.IndexOf("hal", 1, Case.Ignore)); UT_EQ(13, subs.IndexOf("util", 1, Case.Ignore)); UT_EQ(13, subs.IndexOf("UTIL", 5, Case.Ignore)); UT_EQ(13, subs.IndexOf("UTIL", 13, Case.Ignore)); UT_EQ(-1, subs.IndexOf("UTIL", 14, Case.Ignore)); } // ------------------ search one of several characters ------------------ subs.Set("abc@" + "abcd abcde" + "abc@de", 4, 10); { // search one of int l = subs.Length(); UT_EQ(4, subs.IndexOfAny(CString.DefaultWhitespaces, Inclusion.Include)); UT_EQ(-1, subs.IndexOfAny("x".ToCharArray(), Inclusion.Include)); UT_EQ(-1, subs.IndexOfAny("xy".ToCharArray(), Inclusion.Include)); UT_EQ(-1, subs.IndexOfAny("xyz".ToCharArray(), Inclusion.Include)); UT_EQ(3, subs.IndexOfAny("xyd".ToCharArray(), Inclusion.Include)); UT_EQ(3, subs.IndexOfAny("d".ToCharArray(), Inclusion.Include)); UT_EQ(3, subs.IndexOfAny("xyd".ToCharArray(), Inclusion.Include, -2)); UT_EQ(8, subs.IndexOfAny("xyd".ToCharArray(), Inclusion.Include, 4)); UT_EQ(-1, subs.IndexOfAny("xyd".ToCharArray(), Inclusion.Include, 20)); UT_EQ(-1, subs.LastIndexOfAny("".ToCharArray(), Inclusion.Include)); UT_EQ(-1, subs.LastIndexOfAny("x".ToCharArray(), Inclusion.Include)); UT_EQ(-1, subs.LastIndexOfAny("xy".ToCharArray(), Inclusion.Include)); UT_EQ(-1, subs.LastIndexOfAny("xyz".ToCharArray(), Inclusion.Include)); UT_EQ(8, subs.LastIndexOfAny("xyd".ToCharArray(), Inclusion.Include)); UT_EQ(-1, subs.LastIndexOfAny("xyd".ToCharArray(), Inclusion.Include, -2)); UT_EQ(-1, subs.LastIndexOfAny("xyd".ToCharArray(), Inclusion.Include, 2)); UT_EQ(3, subs.LastIndexOfAny("xyd".ToCharArray(), Inclusion.Include, 4)); UT_EQ(0, subs.LastIndexOfAny("a".ToCharArray(), Inclusion.Include, 4)); UT_EQ(1, subs.LastIndexOfAny("b".ToCharArray(), Inclusion.Include, 4)); UT_EQ(1, subs.LastIndexOfAny("ba".ToCharArray(), Inclusion.Include, 4)); UT_EQ(0, subs.LastIndexOfAny("xa".ToCharArray(), Inclusion.Include, 4)); UT_EQ(8, subs.LastIndexOfAny("xyd".ToCharArray(), Inclusion.Include, 20)); UT_EQ(8, subs.LastIndexOfAny("d".ToCharArray(), Inclusion.Include, 20)); UT_EQ(9, subs.LastIndexOfAny("e".ToCharArray(), Inclusion.Include, 20)); // search NOT one of UT_EQ(0, subs.IndexOfAny("".ToCharArray(), Inclusion.Exclude)); UT_EQ(0, subs.IndexOfAny("x".ToCharArray(), Inclusion.Exclude)); UT_EQ(0, subs.IndexOfAny("xy".ToCharArray(), Inclusion.Exclude)); UT_EQ(0, subs.IndexOfAny("xyz".ToCharArray(), Inclusion.Exclude)); UT_EQ(1, subs.IndexOfAny("a".ToCharArray(), Inclusion.Exclude)); UT_EQ(2, subs.IndexOfAny("ba".ToCharArray(), Inclusion.Exclude)); UT_EQ(3, subs.IndexOfAny("abc".ToCharArray(), Inclusion.Exclude)); UT_EQ(3, subs.IndexOfAny("acb".ToCharArray(), Inclusion.Exclude)); UT_EQ(3, subs.IndexOfAny("cba".ToCharArray(), Inclusion.Exclude)); UT_EQ(3, subs.IndexOfAny("xcba".ToCharArray(), Inclusion.Exclude)); UT_EQ(l - 1, subs.LastIndexOfAny("".ToCharArray(), Inclusion.Exclude)); UT_EQ(l - 1, subs.LastIndexOfAny("x".ToCharArray(), Inclusion.Exclude)); UT_EQ(l - 1, subs.LastIndexOfAny("xy".ToCharArray(), Inclusion.Exclude)); UT_EQ(l - 2, subs.LastIndexOfAny("e".ToCharArray(), Inclusion.Exclude)); UT_EQ(l - 3, subs.LastIndexOfAny("de".ToCharArray(), Inclusion.Exclude)); UT_EQ(l - 4, subs.LastIndexOfAny("cde".ToCharArray(), Inclusion.Exclude)); UT_EQ(l - 4, subs.LastIndexOfAny("ced".ToCharArray(), Inclusion.Exclude)); UT_EQ(l - 4, subs.LastIndexOfAny("ecd".ToCharArray(), Inclusion.Exclude)); UT_EQ(5, subs.LastIndexOfAny("ecd".ToCharArray(), Inclusion.Exclude, 5)); UT_EQ(4, subs.LastIndexOfAny("ecd".ToCharArray(), Inclusion.Exclude, 4)); UT_EQ(1, subs.LastIndexOfAny("acd".ToCharArray(), Inclusion.Exclude, 3)); UT_EQ(-1, subs.LastIndexOfAny("abc".ToCharArray(), Inclusion.Exclude, 2)); UT_EQ(3, subs.LastIndexOfAny("xay".ToCharArray(), Inclusion.Exclude, 3)); UT_EQ(2, subs.LastIndexOfAny("d".ToCharArray(), Inclusion.Exclude, 3)); UT_EQ(-1, subs.LastIndexOfAny("a".ToCharArray(), Inclusion.Exclude, 0)); } }
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)); } }
// ############################################################################################# // file IO // ############################################################################################# /** **************************************************************************************** * Clears all configuration data and reads the file. It might happen that lines are * ignored or otherwise marked as faulty. All numbers of such lines get collected in * field LinesWithReadErrors. * @return Returns the #Status of the operation. ******************************************************************************************/ public IniFile.Status ReadFile() { Clear(); LastStatus = Status.OK; // read all variables StreamReader file; try { file = new StreamReader(FileName.ToString()); } catch (Exception) { return(LastStatus = Status.ERROR_OPENING_FILE); } String lineS; AString name = new AString(); AString value = new AString(); AString comments = new AString(); Section actSection = Sections[0]; Substring line = new Substring(); Tokenizer tn = new Tokenizer(); int lineNo = 0; bool fileHeaderRead = false; LinesWithReadErrors.Clear(); while ((lineS = file.ReadLine()) != null) { lineNo = 0; // place in AString line.Set(lineS).Trim(); // empty line? if (line.IsEmpty()) { // already collecting a comment? if (comments.IsNotEmpty()) { // first empty line in file found? if (!fileHeaderRead) { //store comments belonging to file fileHeaderRead = true; FileComments = comments; comments = new AString(); continue; } comments.NewLine(); } continue; } // comments line: find comment character '#', ';' or // if (startsWithCommentSymbol(line)) { //gather in comments string if (comments.IsNotEmpty()) { comments.NewLine(); } line.CopyTo(comments, true); continue; } // section line if (line.Consume('[')) { fileHeaderRead = true; // we do not care if there is no closing bracket. But if there is one, we remove it. if (!line.ConsumeFromEnd(']')) { LinesWithReadErrors.Add(lineNo); } // search the section in our section list (if section existed already, new comments // are dropped) actSection = SearchOrCreateSection(line, comments); comments.Clear(); continue; } // variable line? If not, we just drop the line! tn.Set(line, '='); tn.Next(); if (!tn.HasNext()) { LinesWithReadErrors.Add(lineNo); continue; } tn.Actual.CopyTo(name); if (tn.GetRest().IsEmpty()) { LinesWithReadErrors.Add(lineNo); continue; } value.Clear(); Substring valueRead = tn.Actual; // read continues as long as lines end with '\' (must not be '\\') char delim = '\0'; while (valueRead.CharAtEnd() == '\\' && valueRead.CharAtEnd(1) != '\\') { // search end before '\'. The first of all broken lines determines the delimiter valueRead.End--; valueRead.TrimEnd(); if (delim == 0) { delim = valueRead.CharAtEnd(); if (delim == '\"' || char.IsLetterOrDigit(delim)) { delim = '\0'; } } removeEscapeSequences(valueRead, value); if ((lineS = file.ReadLine()) == null) { // last line of the file ended with '\' ! valueRead.Clear(); break; } valueRead.Set(lineS).Trim(); } removeEscapeSequences(valueRead, value); actSection.Insert(name, value, comments).Delim = delim; comments.Clear(); } file.Close(); return(LastStatus); }