-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed bug from issue #2: Remove escape char in strings with escaped s…
…pecial characters
- Loading branch information
Showing
7 changed files
with
171 additions
and
52 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
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
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,98 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017 tools4j.org (Marco Terzer) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.tools4j.spockito; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* Contains static utility methods dealing with strings. | ||
*/ | ||
final class Strings { | ||
|
||
static final Pattern UNESCAPED_COMMA = Pattern.compile("(?<=[^\\\\]),"); | ||
static final Pattern UNESCAPED_SEMICOLON = Pattern.compile("(?<=[^\\\\]);"); | ||
static final Pattern UNESCAPED_COLON = Pattern.compile("(?<=[^\\\\]):"); | ||
static final Pattern UNESCAPED_EQUAL = Pattern.compile("(?<=[^\\\\])="); | ||
|
||
static String firstCharToUpperCase(final String value) { | ||
return value.length() > 0 ? Character.toUpperCase(value.charAt(0)) + value.substring(1) : value; | ||
} | ||
|
||
static String removeSurroundingPipes(final String s) { | ||
return removeStartAndEndChars(s, '|', '|'); | ||
} | ||
|
||
static String removeStartAndEndChars(final String s, final char startQuoteChar, final char endQuoteChar) { | ||
final int len = s.length(); | ||
if (len >= 2 && s.charAt(0) == startQuoteChar && s.charAt(len - 1) == endQuoteChar) { | ||
return s.substring(1, len - 1); | ||
} | ||
return s; | ||
} | ||
|
||
static boolean allCharsMatchingAnyOf(final String s, final char ch1, final char ch2) { | ||
final int len = s.length(); | ||
for (int i = 0; i < len; i++) { | ||
if (s.charAt(i) != ch1 && s.charAt(i) != ch2) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
static String unescape(final String s) { | ||
return unescape(s, ',', ';', '|', '=', '\\','\''); | ||
} | ||
|
||
static String unescape(final String s, final char... escapedChars) { | ||
int index = s.indexOf('\\'); | ||
if (index < 0) { | ||
return s; | ||
} | ||
final StringBuilder sb = new StringBuilder(s); | ||
while (index >= 0 && index + 1 < sb.length()) { | ||
final char ch = sb.charAt(index + 1); | ||
if (isCharAnyOf(ch, escapedChars)) { | ||
sb.delete(index, index + 1); | ||
index = sb.indexOf("\\", index + 1); | ||
} else { | ||
index = sb.indexOf("\\", index + 2); | ||
} | ||
} | ||
return s.length() == sb.length() ? s : sb.toString(); | ||
} | ||
|
||
static boolean isCharAnyOf(final char ch, final char... chars) { | ||
for (final char c : chars) { | ||
if (ch == c) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private Strings() { | ||
throw new RuntimeException("No Strings for you!"); | ||
} | ||
} |
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
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
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
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,55 @@ | ||
/** | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017 tools4j.org (Marco Terzer) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package org.tools4j.spockito; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Unit test for {@link Strings#unescape(String)} | ||
*/ | ||
public class UnescapeTest { | ||
|
||
@Test | ||
public void shoudUnescape() { | ||
Assert.assertEquals(",", Strings.unescape("\\,")); | ||
Assert.assertEquals(";", Strings.unescape("\\;")); | ||
Assert.assertEquals("|", Strings.unescape("\\|")); | ||
Assert.assertEquals("=", Strings.unescape("\\=")); | ||
Assert.assertEquals("\\", Strings.unescape("\\\\")); | ||
Assert.assertEquals("\'", Strings.unescape("\\'")); | ||
Assert.assertEquals("123|456", Strings.unescape("123\\|456")); | ||
} | ||
|
||
@Test | ||
public void shoudNotUnescape() { | ||
Assert.assertEquals("\\blabla", Strings.unescape("\\blabla")); | ||
Assert.assertEquals("\\123", Strings.unescape("\\123")); | ||
} | ||
|
||
@Test | ||
public void shoudUnescapeMultiple() { | ||
Assert.assertEquals("\\,123|456;\\a\\;\\8bla'\\'xxx", Strings.unescape("\\\\,123\\|456\\;\\a\\\\\\;\\8bla\\'\\\\'xxx")); | ||
} | ||
} |