|
| 1 | +/******************************************************************************* |
| 2 | +
|
| 3 | + NLocalizer (C) 2010-2012 Chris Prusik (KAP1 Ltd www.kap1.net) |
| 4 | + The fast, simple solution for localizing .NET applications, by text files. |
| 5 | + Latest version: http://NLocalizer.codeplex.com/ |
| 6 | +
|
| 7 | + $Id$ |
| 8 | +
|
| 9 | + This library is free software; you can redistribute it and/or |
| 10 | + modify it under the terms of the GNU Lesser General Public |
| 11 | + License as published by the Free Software Foundation; either |
| 12 | + version 2.1 of the License, or (at your option) any later version. |
| 13 | +
|
| 14 | + This library is distributed in the hope that it will be useful, |
| 15 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | + Lesser General Public License for more details. |
| 18 | +
|
| 19 | +*******************************************************************************/ |
| 20 | +using System; |
| 21 | + |
| 22 | +namespace NLocalizer |
| 23 | +{ |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Text finder static class. |
| 27 | + /// </summary> |
| 28 | + public static class Finder |
| 29 | + { |
| 30 | + /// <summary> |
| 31 | + /// Find searchText in text from begin pos. |
| 32 | + /// </summary> |
| 33 | + /// <param name="text">Searched text.</param> |
| 34 | + /// <param name="searchText">Text to find.</param> |
| 35 | + /// <param name="begin">Start cursor positon.</param> |
| 36 | + /// <param name="forward">Forward search (true) or reverse (false).</param> |
| 37 | + /// <returns></returns> |
| 38 | + public static int IndexOf(string text, string searchText, int begin, bool forward) |
| 39 | + { |
| 40 | + if (forward) |
| 41 | + return text.IndexOf(searchText, begin, StringComparison.Ordinal); |
| 42 | + while (begin >= 0) |
| 43 | + { |
| 44 | + if (text.Length >= begin + searchText.Length && |
| 45 | + searchText == text.Substring(begin, searchText.Length)) |
| 46 | + return begin; |
| 47 | + begin--; |
| 48 | + } |
| 49 | + return -1; |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Remove all string from text beetween before and after. |
| 54 | + /// </summary> |
| 55 | + /// <param name="text"></param> |
| 56 | + /// <param name="before"></param> |
| 57 | + /// <param name="after"></param> |
| 58 | + /// <returns></returns> |
| 59 | + public static string RemoveBeetweenWithout(string text, string before, string after) |
| 60 | + { |
| 61 | + int begin = 0; |
| 62 | + if (text != "") |
| 63 | + { |
| 64 | + if (before != "") |
| 65 | + { |
| 66 | + begin = text.IndexOf(before, begin, StringComparison.Ordinal); |
| 67 | + if (begin >= 0) |
| 68 | + begin = begin + before.Length; |
| 69 | + else |
| 70 | + begin = text.Length; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + int end = text.Length; |
| 75 | + if (after != "") |
| 76 | + { |
| 77 | + end = text.IndexOf(after, Math.Min(begin + 1, text.Length - 1), StringComparison.Ordinal); |
| 78 | + if (end < 0) |
| 79 | + end = text.Length; |
| 80 | + } |
| 81 | + string result = text.Remove(begin, end - begin); |
| 82 | + return result; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Remove all string from text beetween before and after. |
| 87 | + /// </summary> |
| 88 | + /// <param name="text"></param> |
| 89 | + /// <param name="before"></param> |
| 90 | + /// <param name="after"></param> |
| 91 | + /// <returns></returns> |
| 92 | + public static string RemoveBeetweenWithAll(string text, string before, string after) |
| 93 | + { |
| 94 | + string result = text; |
| 95 | + int len; |
| 96 | + do |
| 97 | + { |
| 98 | + len = result.Length; |
| 99 | + result = RemoveBeetweenWith(result, before, after); |
| 100 | + } |
| 101 | + while (len > result.Length); |
| 102 | + return result; |
| 103 | + } |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Remove all string from text beetween before and after. |
| 107 | + /// </summary> |
| 108 | + /// <param name="text"></param> |
| 109 | + /// <param name="before"></param> |
| 110 | + /// <param name="after"></param> |
| 111 | + /// <returns></returns> |
| 112 | + public static string RemoveBeetweenWith(string text, string before, string after) |
| 113 | + { |
| 114 | + int begin = 0; |
| 115 | + if (text != "") |
| 116 | + { |
| 117 | + if (before != "") |
| 118 | + { |
| 119 | + begin = text.IndexOf(before, begin, StringComparison.Ordinal); |
| 120 | + if (begin < 0) |
| 121 | + begin = text.Length; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + int end = text.Length; |
| 126 | + if (after != "") |
| 127 | + { |
| 128 | + end = text.IndexOf(after, Math.Min(begin + 1, text.Length - 1), StringComparison.Ordinal); |
| 129 | + if (end < 0) |
| 130 | + end = text.Length; |
| 131 | + } |
| 132 | + if (begin != text.Length || end != text.Length) |
| 133 | + return text.Remove(begin, end - begin + after.Length); |
| 134 | + return text; |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Find string beetween before and after in text. |
| 139 | + /// </summary> |
| 140 | + /// <param name="text">Searched text.</param> |
| 141 | + /// <param name="before"></param> |
| 142 | + /// <param name="after"></param> |
| 143 | + /// <param name="forward">Forward search (true) or reverse (false).</param> |
| 144 | + /// <returns></returns> |
| 145 | + public static string FindBeetween(string text, string before, string after, bool forward) |
| 146 | + { |
| 147 | + int begin = 0; |
| 148 | + return FindBeetween(text, before, after, ref begin, forward); |
| 149 | + } |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Find string beetween before and after in text. |
| 153 | + /// </summary> |
| 154 | + /// <param name="text">Searched text.</param> |
| 155 | + /// <param name="before"></param> |
| 156 | + /// <param name="after"></param> |
| 157 | + /// <returns></returns> |
| 158 | + public static string FindBeetween(string text, string before, string after) |
| 159 | + { |
| 160 | + int begin = 0; |
| 161 | + return FindBeetween(text, before, after, ref begin, true); |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Find string beetween before and after in text. |
| 166 | + /// </summary> |
| 167 | + /// <param name="text">Searched text.</param> |
| 168 | + /// <param name="before"></param> |
| 169 | + /// <param name="after"></param> |
| 170 | + /// <param name="begin">Start cursor positon.</param> |
| 171 | + /// <returns></returns> |
| 172 | + public static string FindBeetween(string text, string before, string after, ref int begin) |
| 173 | + { |
| 174 | + return FindBeetween(text, before, after, ref begin, true); |
| 175 | + } |
| 176 | + |
| 177 | + /// <summary> |
| 178 | + /// Find string beetween before and after in text. |
| 179 | + /// </summary> |
| 180 | + /// <param name="text">Searched text.</param> |
| 181 | + /// <param name="before"></param> |
| 182 | + /// <param name="after"></param> |
| 183 | + /// <param name="begin">Start cursor positon.</param> |
| 184 | + /// <param name="forward">Forward search (true) or reverse (false).</param> |
| 185 | + /// <returns></returns> |
| 186 | + public static string FindBeetween(string text, string before, string after, ref int begin, bool forward) |
| 187 | + { |
| 188 | + if (text != "") |
| 189 | + { |
| 190 | + if (before != "") |
| 191 | + { |
| 192 | + begin = IndexOf(text, before, Math.Min(begin, text.Length - 1), forward); |
| 193 | + if (begin >= 0) |
| 194 | + begin = begin + before.Length; |
| 195 | + else |
| 196 | + begin = text.Length; |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + int end = text.Length; |
| 201 | + if (after != "") |
| 202 | + { |
| 203 | + end = IndexOf(text, after, Math.Min(begin, text.Length - 1), forward); |
| 204 | + if (end < 0) |
| 205 | + end = text.Length; |
| 206 | + } |
| 207 | + string result = text.Substring(begin, end - begin); |
| 208 | + begin += result.Length; |
| 209 | + return result; |
| 210 | + } |
| 211 | + |
| 212 | + /// <summary> |
| 213 | + /// Find string beetween before and after in text. |
| 214 | + /// </summary> |
| 215 | + /// <param name="text">Searched text.</param> |
| 216 | + /// <param name="before"></param> |
| 217 | + /// <param name="after"></param> |
| 218 | + /// <returns></returns> |
| 219 | + public static string FindBeetween(string text, string[] before, string after) |
| 220 | + { |
| 221 | + int begin = 0; |
| 222 | + return FindBeetween(text, before, after, ref begin, true); |
| 223 | + } |
| 224 | + |
| 225 | + /// <summary> |
| 226 | + /// Find string beetween before and after in text. |
| 227 | + /// </summary> |
| 228 | + /// <param name="text">Searched text.</param> |
| 229 | + /// <param name="before"></param> |
| 230 | + /// <param name="after"></param> |
| 231 | + /// <param name="forward">Forward search (true) or reverse (false).</param> |
| 232 | + /// <returns></returns> |
| 233 | + public static string FindBeetween(string text, string[] before, string after, bool forward) |
| 234 | + { |
| 235 | + int begin = 0; |
| 236 | + return FindBeetween(text, before, after, ref begin, forward); |
| 237 | + } |
| 238 | + |
| 239 | + /// <summary> |
| 240 | + /// Find string beetween before and after in text. |
| 241 | + /// </summary> |
| 242 | + /// <param name="text">Searched text.</param> |
| 243 | + /// <param name="before"></param> |
| 244 | + /// <param name="after"></param> |
| 245 | + /// <param name="begin">Start cursor positon.</param> |
| 246 | + /// <param name="forward">Forward search (true) or reverse (false).</param> |
| 247 | + /// <returns></returns> |
| 248 | + public static string FindBeetween(string text, string[] before, string after, ref int begin, bool forward) |
| 249 | + { |
| 250 | + int minPos = text.Length; |
| 251 | + string result = ""; |
| 252 | + for (int i = 0; i < before.Length; i++) |
| 253 | + { |
| 254 | + int pos = begin; |
| 255 | + string tempResult = FindBeetween(text, before[i], after, ref pos, forward); |
| 256 | + if (pos < minPos) |
| 257 | + { |
| 258 | + result = tempResult; |
| 259 | + minPos = pos; |
| 260 | + } |
| 261 | + } |
| 262 | + begin = minPos; |
| 263 | + return result; |
| 264 | + } |
| 265 | + } |
| 266 | +} |
0 commit comments