// NAnt - A .NET build tool // Copyright (C) 2001-2003 Gerry Shaw // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // Ian Maclean (imaclean@gmail.com) // Jaroslaw Kowalski (jkowalski@users.sourceforge.net) using System; using System.IO; using System.Collections; using System.Reflection; using System.Globalization; using NAnt.Core; using NAnt.Core.Types; using NAnt.Core.Attributes; namespace NAnt.Core.Functions { [FunctionSet("string", "String")] public class StringFunctions : FunctionSetBase { #region Public Instance Constructors public StringFunctions(Project project, PropertyDictionary propDict) : base(project, propDict) { } #endregion Public Instance Constructors #region Public Static Methods /// /// Returns the length of the specified string. /// /// input string /// /// The string's length. /// /// /// string::get-length('foo') ==> 3 /// /// /// string::get-length('') ==> 0 /// [Function("get-length")] public static int GetLength(string s) { return s.Length; } /// /// Returns a substring of the specified string. /// /// input string /// position of the start of the substring /// the length of the substring /// /// /// If the is greater than zero, the /// function returns a substring starting at character position /// with a length of /// characters. /// /// /// If the is equal to zero, the function /// returns an empty string. /// /// /// or is less than zero. /// is greater than the length of . /// plus indicates a position not within . /// /// string::substring('testing string', 0, 4) ==> 'test' /// /// /// string::substring('testing string', 8, 3) ==> 'str' /// /// /// string::substring('testing string', 8, 0) ==> '' /// /// /// string::substring('testing string', -1, 5) ==> ERROR /// /// /// string::substring('testing string', 8, -1) ==> ERROR /// /// /// string::substring('testing string', 5, 17) ==> ERROR /// [Function("substring")] public static string Substring(string str, int startIndex, int length) { return str.Substring(startIndex, length); } /// /// Tests whether the specified string starts with the specified prefix /// string. /// /// test string /// prefix string /// /// when is a prefix for /// the string . Meaning, the characters at the /// beginning of are identical to /// ; otherwise, . /// /// /// This function performs a case-sensitive word search using the /// invariant culture. /// /// /// string::starts-with('testing string', 'test') ==> true /// /// /// string::starts-with('testing string', 'testing') ==> true /// /// /// string::starts-with('testing string', 'string') ==> false /// /// /// string::starts-with('test', 'testing string') ==> false /// [Function("starts-with")] public static bool StartsWith(string s1, string s2) { return CultureInfo.InvariantCulture.CompareInfo.IsPrefix(s1, s2); } /// /// Tests whether the specified string ends with the specified suffix /// string. /// /// test string /// suffix string /// /// when is a suffix for /// the string . Meaning, the characters at the /// end of are identical to /// ; otherwise, . /// /// /// This function performs a case-sensitive word search using the /// invariant culture. /// /// /// string::ends-with('testing string', 'string') ==> true /// /// /// string::ends-with('testing string', '') ==> true /// /// /// string::ends-with('testing string', 'bring') ==> false /// /// /// string::ends-with('string', 'testing string') ==> false /// [Function("ends-with")] public static bool EndsWith(string s1, string s2) { return CultureInfo.InvariantCulture.CompareInfo.IsSuffix(s1, s2); } /// /// Returns the specified string converted to lowercase. /// /// input string /// /// The string in lowercase. /// /// /// The casing rules of the invariant culture are used to convert the /// to lowercase. /// /// /// string::to-lower('testing string') ==> 'testing string' /// /// /// string::to-lower('Testing String') ==> 'testing string' /// /// /// string::to-lower('Test 123') ==> 'test 123' /// [Function("to-lower")] public static string ToLower(string s) { return s.ToLower(CultureInfo.InvariantCulture); } /// /// Returns the specified string converted to uppercase. /// /// input string /// /// The string in uppercase. /// /// /// The casing rules of the invariant culture are used to convert the /// to uppercase. /// /// /// string::to-upper('testing string') ==> 'TESTING STRING' /// /// /// string::to-upper('Testing String') ==> 'TESTING STRING' /// /// /// string::to-upper('Test 123') ==> 'TEST 123' /// [Function("to-upper")] public static string ToUpper(string s) { return s.ToUpper(CultureInfo.InvariantCulture); } /// /// Returns a string corresponding to the replacement of a given string /// with another in the specified string. /// /// input string /// A to be replaced. /// A to replace all occurrences of . /// /// A equivalent to but /// with all instances of replaced with /// . /// /// is an empty string. /// /// This function performs a word (case-sensitive and culture-sensitive) /// search to find . /// /// /// string::replace('testing string', 'test', 'winn') ==> 'winning string' /// /// /// string::replace('testing string', 'foo', 'winn') ==> 'testing string' /// /// /// string::replace('testing string', 'ing', '') ==> 'test str' /// /// /// string::replace('banana', 'ana', 'ana') ==> 'banana' /// [Function("replace")] public static string Replace(string str, string oldValue, string newValue) { return str.Replace(oldValue, newValue); } /// /// Tests whether the specified string contains the given search string. /// /// The string to search. /// The string to locate within . /// /// if is found in /// ; otherwise, . /// /// /// This function performs a case-sensitive word search using the /// invariant culture. /// /// /// string::contains('testing string', 'test') ==> true /// /// /// string::contains('testing string', '') ==> true /// /// /// string::contains('testing string', 'Test') ==> false /// /// /// string::contains('testing string', 'foo') ==> false /// [Function("contains")] public static bool Contains(string source, string value) { return CultureInfo.InvariantCulture.CompareInfo.IndexOf(source, value, CompareOptions.None) >= 0; } /// /// Returns the position of the first occurrence in the specified string /// of the given search string. /// /// The string to search. /// The string to locate within . /// /// /// The lowest-index position of in /// if it is found, or -1 if /// does not contain . /// /// /// If is an empty string, the return value /// will always be 0. /// /// /// /// This function performs a case-sensitive word search using the /// invariant culture. /// /// /// string::index-of('testing string', 'test') ==> 0 /// /// /// string::index-of('testing string', '') ==> 0 /// /// /// string::index-of('testing string', 'Test') ==> -1 /// /// /// string::index-of('testing string', 'ing') ==> 4 /// [Function("index-of")] public static int IndexOf(string source, string value) { return CultureInfo.InvariantCulture.CompareInfo.IndexOf(source, value, CompareOptions.None); } /// /// Returns the position of the last occurrence in the specified string /// of the given search string. /// /// The string to search. /// The string to locate within . /// /// /// The highest-index position of in /// if it is found, or -1 if /// does not contain . /// /// /// If is an empty string, the return value /// is the last index position in . /// /// /// /// This function performs a case-sensitive word search using the /// invariant culture. /// /// /// string::last-index-of('testing string', 'test') ==> 0 /// /// /// string::last-index-of('testing string', '') ==> 13 /// /// /// string::last-index-of('testing string', 'Test') ==> -1 /// /// /// string::last-index-of('testing string', 'ing') ==> 11 /// [Function("last-index-of")] public static int LastIndexOf(string source, string value) { return CultureInfo.InvariantCulture.CompareInfo.LastIndexOf(source, value, CompareOptions.None); } /// /// Returns the given string left-padded to the given length. /// /// The that needs to be left-padded. /// The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters. /// A Unicode padding character. /// /// If the length of is at least /// , then a new identical /// to is returned. Otherwise, /// will be padded on the left with as many /// characters as needed to create a length of . /// /// is less than zero. /// /// Note that only the first character of /// will be used when padding the result. /// /// /// string::pad-left('test', 10, ' ') ==> ' test' /// /// /// string::pad-left('test', 10, 'test') ==> 'tttttttest' /// /// /// string::pad-left('test', 3, ' ') ==> 'test' /// /// /// string::pad-left('test', -4, ' ') ==> ERROR /// [Function("pad-left")] public static string PadLeft(string s, int totalWidth, string paddingChar) { return s.PadLeft(totalWidth, paddingChar[0]); } /// /// Returns the given string right-padded to the given length. /// /// The that needs to be right-padded. /// The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters. /// A Unicode padding character. /// /// If the length of is at least /// , then a new identical /// to is returned. Otherwise, /// will be padded on the right with as many /// characters as needed to create a length of . /// /// is less than zero. /// /// Note that only the first character of /// will be used when padding the result. /// /// /// string::pad-right('test', 10, ' ') ==> 'test ' /// /// /// string::pad-right('test', 10, 'abcd') ==> 'testaaaaaa' /// /// /// string::pad-right('test', 3, ' ') ==> 'test' /// /// /// string::pad-right('test', -3, ' ') ==> ERROR /// [Function("pad-right")] public static string PadRight(string s, int totalWidth, string paddingChar) { return s.PadRight(totalWidth, paddingChar[0]); } /// /// Returns the given string trimmed of whitespace. /// /// input string /// /// The string with any leading or trailing /// white space characters removed. /// /// /// string::trim(' test ') ==> 'test' /// /// /// string::trim('\t\tfoo \r\n') ==> 'foo' /// [Function("trim")] public static string Trim(string s) { return s.Trim(); } /// /// Returns the given string trimmed of leading whitespace. /// /// input string /// /// The string with any leading /// whites pace characters removed. /// /// /// string::trim-start(' test ') ==> 'test ' /// /// /// string::trim-start('\t\tfoo \r\n') ==> 'foo \r\n' /// [Function("trim-start")] public static string TrimStart(string s) { return s.TrimStart(); } /// /// Returns the given string trimmed of trailing whitespace. /// /// input string /// /// The string with any trailing /// white space characters removed. /// /// /// string::trim-end(' test ') ==> ' test' /// /// /// string::trim-end('\t\tfoo \r\n') ==> '\t\tfoo' /// [Function("trim-end")] public static string TrimEnd(string s) { return s.TrimEnd(); } #endregion Public Static Methods } }