[System.Net] Add support for .pac proxy config scripts on mac
[mono.git] / mcs / class / System / System.Text.RegularExpressions / JavaUtils.jvm.cs
1 //
2 // JavaUtils.jvm.cs
3 //
4 // Author:
5 //      Arina Itkes  <arinai@mainsoft.com>
6 //
7 // Copyright (C) 2007 Mainsoft, Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Collections.Generic;
33 using System.Text;
34 using java.util.regex;
35 using java.lang;
36
37 namespace System.Text.RegularExpressions
38 {
39         sealed class JavaUtils
40         {
41                 private const string ASTERISK_PATTERN = @"\*";
42                 private static readonly Pattern _asteriskPattern = Pattern.compile (ASTERISK_PATTERN);
43
44                 internal static bool IsMatch (string input, string pattern) {
45                         Pattern p = Pattern.compile (pattern);
46                         Matcher m = p.matcher ((CharSequence) (object) input);
47
48                         return m.find ();
49                 }
50
51                 internal static Matcher Matcher (string input, string pattern) {
52                         Pattern p = Pattern.compile (pattern);
53                         return p.matcher ((CharSequence) (object) input);
54                 }
55
56                 internal static string ReplaceAll (string input, string pattern, string replacement) {
57                         Pattern p = Pattern.compile (pattern);
58                         Matcher m = p.matcher ((CharSequence) (object) input);
59                         return m.replaceAll (replacement);
60                 }
61
62                 internal static string ReplaceAllAdvanced (string input, string pattern, string replacement) {
63                         Matcher m = _asteriskPattern.matcher ((CharSequence) (object) pattern);
64                         string readyPattern = m.replaceAll ("{0," + pattern.Length + "}");
65
66                         return ReplaceAll (input, readyPattern, replacement);
67                 }
68
69                 internal static int GroupCount (string pattern) {
70                         Pattern javaPattern = Pattern.compile (pattern);
71                         Matcher emptyMatcher = javaPattern.matcher ((CharSequence) (object) String.Empty);
72                         return emptyMatcher.groupCount ();
73                 }
74
75                 internal static string ReplaceWithLookBehind (string input, string pattern, string lookBehindPattern, string replacement) {
76                         Pattern p = Pattern.compile (pattern);
77                         Pattern behindPattern = Pattern.compile (lookBehindPattern);
78         
79                         Matcher m = p.matcher ((CharSequence) (object) input);
80                         StringBuffer sb = new StringBuffer ();
81
82                         while (m.find ()) {
83                                 Matcher pm = behindPattern.matcher ((CharSequence) (object) input.Substring (0, m.start()));
84                                 if(pm.find())
85                                         m.appendReplacement(sb, replacement);
86                         }
87
88                         m.appendTail (sb);
89                         return sb.ToString();
90                 }
91         }
92 }