-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added extension methods to Regex and CharSequence (#17)
* Added Regex.remove(), Regex.removeFirst(), Regex.removeLast() and unit tests * Added CharSequence.remove(), CharSequence.removeFirst(), CharSequence.removeLast() and unit tests
- Loading branch information
1 parent
49451fa
commit 79aa70e
Showing
4 changed files
with
431 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package uk.co.mainwave.regextoolboxkotlin.extensions | ||
|
||
/** | ||
* Remove all matches of the supplied [regex] from this string, returning the modified string. | ||
* If no match for the Regex is found in this string, an unmodified copy of this string is returned. | ||
*/ | ||
fun CharSequence.remove(regex: Regex): String = regex.remove(this) | ||
|
||
/** | ||
* Remove the first match of the supplied [regex] from this string, returning the modified string. | ||
* If no match for the Regex is found in this string, an unmodified copy of this string is returned. | ||
*/ | ||
fun CharSequence.removeFirst(regex: Regex): String = regex.removeFirst(this) | ||
|
||
/** | ||
* Remove the last match of the supplied [regex] from this string, returning the modified string. | ||
* If no match for the Regex is found in this string, an unmodified copy of this string is returned. | ||
*/ | ||
fun CharSequence.removeLast(regex: Regex): String = regex.removeLast(this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package uk.co.mainwave.regextoolboxkotlin.extensions | ||
|
||
/** | ||
* Remove all matches of this [Regex] from the supplied [input] string, returning the modified string. | ||
* If no match for the Regex is found in the input string, the input string is returned unmodified. | ||
*/ | ||
fun Regex.remove(input: CharSequence): String = this.replace(input, "") | ||
|
||
/** | ||
* Remove the first match of this [Regex] from the supplied [input] string, returning the modified string. | ||
* If no match for the Regex is found in the input string, the input string is returned unmodified. | ||
*/ | ||
fun Regex.removeFirst(input: CharSequence): String = this.replaceFirst(input, "") | ||
|
||
/** | ||
* Remove the last match of this [Regex] from the supplied [input] string, returning the modified string. | ||
* If no match for the Regex is found in the input string, the input string is returned unmodified. | ||
*/ | ||
fun Regex.removeLast(input: CharSequence): String = this.findAll(input).let { matches -> | ||
when (matches.any()) { | ||
true -> input.removeRange(matches.last().range) | ||
false -> input | ||
}.toString() | ||
} |
194 changes: 194 additions & 0 deletions
194
src/test/kotlin/extensions/CharSequenceExtensionsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package uk.co.mainwave.regextoolboxkotlin.extensions | ||
|
||
import org.junit.Assert | ||
import org.junit.Test | ||
import uk.co.mainwave.regextoolboxkotlin.RegexQuantifier | ||
import uk.co.mainwave.regextoolboxkotlin.regex | ||
|
||
class CharSequenceExtensionsTest { | ||
@Test | ||
fun `GIVEN input string containing one regex match WHEN remove() THEN match is removed from string`() { | ||
// Given | ||
val input = "Hello there friendly world" | ||
val regex = regex { | ||
whitespace() | ||
text("there") | ||
} | ||
|
||
// When | ||
val actualResult = input.remove(regex) | ||
|
||
// Then | ||
Assert.assertEquals("Hello friendly world", actualResult) | ||
} | ||
|
||
@Test | ||
fun `GIVEN input string containing one regex match WHEN removeFirst() THEN match is removed from string`() { | ||
// Given | ||
val input = "Hello there friendly world" | ||
val regex = regex { | ||
whitespace() | ||
text("there") | ||
} | ||
|
||
// When | ||
val actualResult = input.removeFirst(regex) | ||
|
||
// Then | ||
Assert.assertEquals("Hello friendly world", actualResult) | ||
} | ||
|
||
@Test | ||
fun `GIVEN input string containing one regex match WHEN removeLast() THEN match is removed from string`() { | ||
// Given | ||
val input = "Hello there friendly world" | ||
val regex = regex { | ||
whitespace() | ||
text("there") | ||
} | ||
|
||
// When | ||
val actualResult = input.removeLast(regex) | ||
|
||
// Then | ||
Assert.assertEquals("Hello friendly world", actualResult) | ||
} | ||
|
||
@Test | ||
fun `GIVEN input string containing multiple regex matches WHEN remove() THEN all matches are removed from string`() { | ||
// Given | ||
val input = "Hello there friendly world" | ||
val regex = regex { | ||
whitespace() | ||
letter(RegexQuantifier.OneOrMore) | ||
} | ||
|
||
// When | ||
val actualResult = input.remove(regex) | ||
|
||
// Then | ||
Assert.assertEquals("Hello", actualResult) | ||
} | ||
|
||
@Test | ||
fun `GIVEN input string containing multiple regex matches WHEN removeFirst() THEN first match is removed from string`() { | ||
// Given | ||
val input = "Hello there friendly world" | ||
val regex = regex { | ||
whitespace() | ||
letter(RegexQuantifier.OneOrMore) | ||
} | ||
|
||
// When | ||
val actualResult = input.removeFirst(regex) | ||
|
||
// Then | ||
Assert.assertEquals("Hello friendly world", actualResult) | ||
} | ||
|
||
@Test | ||
fun `GIVEN input string containing multiple regex matches WHEN removeLast() THEN last match is removed from string`() { | ||
// Given | ||
val input = "Hello there friendly world" | ||
val regex = regex { | ||
whitespace() | ||
letter(RegexQuantifier.OneOrMore) | ||
} | ||
|
||
// When | ||
val actualResult = input.removeLast(regex) | ||
|
||
// Then | ||
Assert.assertEquals("Hello there friendly", actualResult) | ||
} | ||
|
||
@Test | ||
fun `GIVEN input string containing no regex matches WHEN remove() THEN string is returned unaltered`() { | ||
// Given | ||
val input = "Hello there friendly world" | ||
val regex = regex { | ||
digit(RegexQuantifier.OneOrMore) | ||
} | ||
|
||
// When | ||
val actualResult = input.remove(regex) | ||
|
||
// Then | ||
Assert.assertEquals("Hello there friendly world", actualResult) | ||
} | ||
|
||
@Test | ||
fun `GIVEN empty input string WHEN remove() THEN empty string is returned`() { | ||
// Given | ||
val input = "" | ||
val regex = regex { | ||
digit(RegexQuantifier.OneOrMore) | ||
} | ||
|
||
// When | ||
val actualResult = input.remove(regex) | ||
|
||
// Then | ||
Assert.assertEquals("", actualResult) | ||
} | ||
|
||
@Test | ||
fun `GIVEN input string containing no regex matches WHEN removeFirst() THEN string is returned unaltered`() { | ||
// Given | ||
val input = "Hello there friendly world" | ||
val regex = regex { | ||
digit(RegexQuantifier.OneOrMore) | ||
} | ||
|
||
// When | ||
val actualResult = input.removeFirst(regex) | ||
|
||
// Then | ||
Assert.assertEquals("Hello there friendly world", actualResult) | ||
} | ||
|
||
@Test | ||
fun `GIVEN empty input string WHEN removeFirst() THEN empty string is returned`() { | ||
// Given | ||
val input = "" | ||
val regex = regex { | ||
digit(RegexQuantifier.OneOrMore) | ||
} | ||
|
||
// When | ||
val actualResult = input.removeFirst(regex) | ||
|
||
// Then | ||
Assert.assertEquals("", actualResult) | ||
} | ||
|
||
@Test | ||
fun `GIVEN input string containing no regex matches WHEN removeLast() THEN string is returned unaltered`() { | ||
// Given | ||
val input = "Hello there friendly world" | ||
val regex = regex { | ||
digit(RegexQuantifier.OneOrMore) | ||
} | ||
|
||
// When | ||
val actualResult = input.removeLast(regex) | ||
|
||
// Then | ||
Assert.assertEquals("Hello there friendly world", actualResult) | ||
} | ||
|
||
@Test | ||
fun `GIVEN empty input string WHEN removeLast() THEN empty string is returned`() { | ||
// Given | ||
val input = "" | ||
val regex = regex { | ||
digit(RegexQuantifier.OneOrMore) | ||
} | ||
|
||
// When | ||
val actualResult = input.removeLast(regex) | ||
|
||
// Then | ||
Assert.assertEquals("", actualResult) | ||
} | ||
} |
Oops, something went wrong.