1cf5933a716e7eb6bd5ff49aa62f3e76379ebaf51Chris Lattner/* 2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman * [The "BSD licence"] 3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell * Copyright (c) 2005-2008 Terence Parr 4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell * All rights reserved. 54ee451de366474b9c228b4e5fa573795a715216dChris Lattner * 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner * Conversion to C#: 7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman * Copyright (c) 2008 Sam Harwell, Pixel Mine, Inc. 8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell * All rights reserved. 9237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner * 10befa499d45ffcc32bd9902518aec18589464e47cChris Lattner * Redistribution and use in source and binary forms, with or without 11befa499d45ffcc32bd9902518aec18589464e47cChris Lattner * modification, are permitted provided that the following conditions 12befa499d45ffcc32bd9902518aec18589464e47cChris Lattner * are met: 13237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner * 1. Redistributions of source code must retain the above copyright 14237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner * notice, this list of conditions and the following disclaimer. 15237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner * 2. Redistributions in binary form must reproduce the above copyright 1686453c52ba02e743d29c08456e51006500041456Chris Lattner * notice, this list of conditions and the following disclaimer in the 17237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner * documentation and/or other materials provided with the distribution. 1847b14a4a6a455c7be169cfd312fcbe796f0ad426Misha Brukman * 3. The name of the author may not be used to endorse or promote products 191f67ce4aa3f65619f54c8a3072539da5b0022841Dale Johannesen * derived from this software without specific prior written permission. 20237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner * 21e4aeec003f82a5263ffb168e175e6fca8b6f681dDan Gohman * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 236f7426ec2e46bb19cc9f9e75f1c355b35cf12d7dTanya Lattner * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 254ff4141a9ee9ce705f2a709f3372acaf58d86ea3Chris Lattner * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 264ff4141a9ee9ce705f2a709f3372acaf58d86ea3Chris Lattner * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29ce63ffb52f249b62cdf2d250c128007b13f27e71Daniel Dunbar * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 3012f0babca4459c253675700e1d707652d5b6ba17Chris Lattner * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer */ 32befa499d45ffcc32bd9902518aec18589464e47cChris Lattner 33a51bcb50b0c74adc741361824ef81dbefb715c53Chris Lattnerusing System; 34d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekeusing System.Text; 3586453c52ba02e743d29c08456e51006500041456Chris Lattner 36dbab4dc942e0c3286415908762de71a9447f9dfaChris Lattnernamespace Antlr.Runtime.JavaExtensions 3786453c52ba02e743d29c08456e51006500041456Chris Lattner{ 38199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static class StringExtensions 3986453c52ba02e743d29c08456e51006500041456Chris Lattner { 40844731a7f1909f55935e3514c9e713a62d67662eDan Gohman#if DEBUG 41f9c3b228e5579e0d2a9cd05a2191fe17b4c58b23Jakob Stoklund Olesen [Obsolete] 42f9c3b228e5579e0d2a9cd05a2191fe17b4c58b23Jakob Stoklund Olesen public static char charAt( string str, int index ) 43237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner { 44f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen return str[index]; 45f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen } 46f0907fe59093753fe5a9e8fe5adc399dbdc94627Jakob Stoklund Olesen 47570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen [Obsolete] 48570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen public static bool endsWith( string str, string value ) 49570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen { 50570a4a5d9ca31f276a67502d1e0533d59d331feaJakob Stoklund Olesen return str.EndsWith( value ); 51ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman } 52ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman 53237ef567f6764f24a47c63121cc0a599ddc8f56dChris Lattner [Obsolete] 54ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman public static int indexOf( string str, char value ) 55ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman { 56120d053e3ba810b44047fbcb719824bed5673ca9Chris Lattner return str.IndexOf( value ); 57ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner } 58ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner 59ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner [Obsolete] 60ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner public static int indexOf( string str, char value, int startIndex ) 61ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner { 62ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner return str.IndexOf( value, startIndex ); 63ff2dad312883e5da91fb9f4e3619b7d095867f3bChris Lattner } 64199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 65199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner [Obsolete] 66199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static int indexOf( string str, string value ) 67199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner { 68199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner return str.IndexOf( value ); 69199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 70199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 71199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner [Obsolete] 72cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner public static int indexOf( string str, string value, int startIndex ) 73cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner { 74cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner return str.IndexOf( value, startIndex ); 75cc0a0299d96676e0a51e9b8f5bf617d8025f09a7Chris Lattner } 76199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 77199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner [Obsolete] 78199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static int lastIndexOf( string str, char value ) 79befa499d45ffcc32bd9902518aec18589464e47cChris Lattner { 8066c75aaa028683c389c55b377ee2411b61081677Bill Wendling return str.LastIndexOf( value ); 8166c75aaa028683c389c55b377ee2411b61081677Bill Wendling } 82199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 83199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner [Obsolete] 84199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static int lastIndexOf( string str, string value ) 85199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner { 8612f0babca4459c253675700e1d707652d5b6ba17Chris Lattner return str.LastIndexOf( value ); 87fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman } 888c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling 898c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling [Obsolete] 908c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling public static int length( string str ) 918c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling { 928c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling return str.Length; 938c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling } 948c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling#endif 958c1604e7d617622cb391f1c679ddf70ea03baedcBill Wendling 96199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static string replace( string str, char oldValue, char newValue ) 97199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner { 98199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner int index = str.IndexOf( oldValue ); 99199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner if ( index == -1 ) 100199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner return str; 101199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 102199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner System.Text.StringBuilder builder = new StringBuilder( str ); 103199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner builder[index] = newValue; 104199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner return builder.ToString(); 105199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 106199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 107199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static string replaceAll( string str, string regex, string newValue ) 108199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner { 109199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner return System.Text.RegularExpressions.Regex.Replace( str, regex, newValue ); 110199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 111199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 112199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static string replaceFirst( string str, string regex, string replacement ) 113199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner { 114199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner return System.Text.RegularExpressions.Regex.Replace( str, regex, replacement ); 115199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 116199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 117199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner#if DEBUG 118199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner [Obsolete] 119199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static bool startsWith( string str, string value ) 120199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner { 121199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner return str.StartsWith( value ); 122199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 123199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner#endif 124199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 125199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner [Obsolete] 126199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static string substring( string str, int startOffset ) 127199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner { 128199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner return str.Substring( startOffset ); 129199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 130199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 131199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static string substring( string str, int startOffset, int endOffset ) 132199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner { 133199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner return str.Substring( startOffset, endOffset - startOffset ); 134199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 135199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 136199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner#if DEBUG 137199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner [Obsolete] 138199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static char[] toCharArray( string str ) 139199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner { 140199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner return str.ToCharArray(); 141199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 142199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 143199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner [Obsolete] 144199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static string toUpperCase( string str ) 145199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner { 146199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner return str.ToUpperInvariant(); 147199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 148199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner 149199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner [Obsolete] 150199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner public static string trim( string str ) 151199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner { 152199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner return str.Trim(); 153199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 154199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner#endif 155199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner } 156199ba42cbf56b2fc9c708edb4f08f97dd99ddd49Chris Lattner} 157c0aa67950ae0f6e9611240d8f0e3ac49dc8195c0David Greene