|
6 | 6 | // Copyright (c) 2023 ModestNerds, Co |
7 | 7 | // |
8 | 8 |
|
| 9 | +/// Represents a selection within a string, defined by start and end markers. |
| 10 | +/// |
| 11 | +/// This class allows you to perform operations on a substring that is |
| 12 | +/// identified by string markers rather than integer indices. |
| 13 | +class StringSelection { |
| 14 | + /// Creates a [StringSelection] with the given source string and selection bounds. |
| 15 | + const StringSelection({ |
| 16 | + required this.source, |
| 17 | + required this.startIndex, |
| 18 | + required this.endIndex, |
| 19 | + }); |
| 20 | + |
| 21 | + /// The original source string. |
| 22 | + final String source; |
| 23 | + |
| 24 | + /// The starting index of the selection (inclusive). |
| 25 | + final int startIndex; |
| 26 | + |
| 27 | + /// The ending index of the selection (exclusive). |
| 28 | + final int endIndex; |
| 29 | + |
| 30 | + /// Returns the selected substring. |
| 31 | + /// |
| 32 | + /// Example: |
| 33 | + /// ```dart |
| 34 | + /// final selection = 'Hello <b>World</b>'.select(start: '<b>', end: '</b>'); |
| 35 | + /// print(selection?.selected); // Output: <b>World</b> |
| 36 | + /// ``` |
| 37 | + String get selected => source.substring(startIndex, endIndex); |
| 38 | + |
| 39 | + /// Returns the content between the start and end markers (excluding the markers). |
| 40 | + /// |
| 41 | + /// Example: |
| 42 | + /// ```dart |
| 43 | + /// final selection = 'Hello <b>World</b>'.select(start: '<b>', end: '</b>'); |
| 44 | + /// print(selection?.content); // Output: World |
| 45 | + /// ``` |
| 46 | + String content(String start, String end) { |
| 47 | + return source.substring(startIndex + start.length, endIndex - end.length); |
| 48 | + } |
| 49 | + |
| 50 | + /// Returns a new string with the selected portion removed. |
| 51 | + /// |
| 52 | + /// Example: |
| 53 | + /// ```dart |
| 54 | + /// final result = 'Hello <script>alert(1)</script> World' |
| 55 | + /// .select(start: '<script', end: '</script>') |
| 56 | + /// ?.remove(); |
| 57 | + /// print(result); // Output: Hello World |
| 58 | + /// ``` |
| 59 | + String remove() { |
| 60 | + return source.substring(0, startIndex) + source.substring(endIndex); |
| 61 | + } |
| 62 | + |
| 63 | + /// Returns a new string with the selected portion replaced by [replacement]. |
| 64 | + /// |
| 65 | + /// Example: |
| 66 | + /// ```dart |
| 67 | + /// final result = 'Hello <b>World</b>' |
| 68 | + /// .select(start: '<b>', end: '</b>') |
| 69 | + /// ?.replace('Universe'); |
| 70 | + /// print(result); // Output: Hello Universe |
| 71 | + /// ``` |
| 72 | + String replace(String replacement) { |
| 73 | + return source.substring(0, startIndex) + |
| 74 | + replacement + |
| 75 | + source.substring(endIndex); |
| 76 | + } |
| 77 | + |
| 78 | + /// Returns a new string with all occurrences of the selection removed. |
| 79 | + /// |
| 80 | + /// Example: |
| 81 | + /// ```dart |
| 82 | + /// final result = 'A<x>B<x>C'.select(start: '<x>', end: '<x>')?.removeAll(); |
| 83 | + /// ``` |
| 84 | + String removeAll(String start, String end) { |
| 85 | + String result = source; |
| 86 | + while (true) { |
| 87 | + final selection = result.select(start: start, end: end); |
| 88 | + if (selection == null) break; |
| 89 | + result = selection.remove(); |
| 90 | + } |
| 91 | + return result; |
| 92 | + } |
| 93 | + |
| 94 | + /// Returns a new string with all occurrences of the selection replaced. |
| 95 | + /// |
| 96 | + /// Example: |
| 97 | + /// ```dart |
| 98 | + /// final result = 'A<x>1</x>B<x>2</x>C' |
| 99 | + /// .select(start: '<x>', end: '</x>') |
| 100 | + /// ?.replaceAll(start: '<x>', end: '</x>', replacement: '[]'); |
| 101 | + /// print(result); // Output: A[]B[]C |
| 102 | + /// ``` |
| 103 | + String replaceAll({ |
| 104 | + required String start, |
| 105 | + required String end, |
| 106 | + required String replacement, |
| 107 | + }) { |
| 108 | + String result = source; |
| 109 | + while (true) { |
| 110 | + final selection = result.select(start: start, end: end); |
| 111 | + if (selection == null) break; |
| 112 | + result = selection.replace(replacement); |
| 113 | + } |
| 114 | + return result; |
| 115 | + } |
| 116 | +} |
| 117 | + |
9 | 118 | extension HandyStringExtension on String { |
10 | 119 | /// Converts this string (assumed to be a country code) to a country flag emoji. |
11 | 120 | /// |
@@ -166,4 +275,42 @@ extension HandyStringExtension on String { |
166 | 275 | inputCharacters.sort(); |
167 | 276 | return characters.join().matches(inputCharacters.join()); |
168 | 277 | } |
| 278 | + |
| 279 | + /// Selects a portion of this string between [start] and [end] markers. |
| 280 | + /// |
| 281 | + /// Returns a [StringSelection] that allows you to perform operations |
| 282 | + /// like [StringSelection.remove], [StringSelection.replace], etc. |
| 283 | + /// |
| 284 | + /// Returns `null` if the [start] or [end] markers are not found, or if |
| 285 | + /// [end] appears before [start]. |
| 286 | + /// |
| 287 | + /// Example: |
| 288 | + /// ```dart |
| 289 | + /// final input = 'Check - Check In: Maintenance Due : ZAR 1374.94.' |
| 290 | + /// '<script type="text/javascript">viewGLAccounts(0,2722896);</script>' |
| 291 | + /// ' Check In:'; |
| 292 | + /// |
| 293 | + /// final result = input.select(start: '<script', end: '</script>')?.remove(); |
| 294 | + /// print(result); |
| 295 | + /// // Output: Check - Check In: Maintenance Due : ZAR 1374.94. Check In: |
| 296 | + /// ``` |
| 297 | + /// |
| 298 | + /// Parameters: |
| 299 | + /// - [start]: The string marking the beginning of the selection. |
| 300 | + /// - [end]: The string marking the end of the selection. |
| 301 | + /// |
| 302 | + /// Returns a [StringSelection] or `null` if markers are not found. |
| 303 | + StringSelection? select({required String start, required String end}) { |
| 304 | + final startIndex = indexOf(start); |
| 305 | + if (startIndex == -1) return null; |
| 306 | + |
| 307 | + final endIndex = indexOf(end, startIndex + start.length); |
| 308 | + if (endIndex == -1) return null; |
| 309 | + |
| 310 | + return StringSelection( |
| 311 | + source: this, |
| 312 | + startIndex: startIndex, |
| 313 | + endIndex: endIndex + end.length, |
| 314 | + ); |
| 315 | + } |
169 | 316 | } |
0 commit comments