Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #722 from csmith/styliser
Move static styliser methods to new class.
- Loading branch information
Showing
7 changed files
with
178 additions
and
33 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
100 changes: 100 additions & 0 deletions
100
src/main/java/com/dmdirc/ui/messages/StyledMessageUtils.java
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,100 @@ | ||
package com.dmdirc.ui.messages; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import static com.dmdirc.ui.messages.Styliser.CODE_CHANNEL; | ||
import static com.dmdirc.ui.messages.Styliser.CODE_HYPERLINK; | ||
import static com.dmdirc.ui.messages.Styliser.CODE_NICKNAME; | ||
import static com.dmdirc.ui.messages.Styliser.CODE_SMILIE; | ||
import static com.dmdirc.ui.messages.Styliser.CODE_TOOLTIP; | ||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
/** | ||
* Utilities for dealing with styled messages. | ||
*/ | ||
@Singleton | ||
public class StyledMessageUtils { | ||
|
||
@Inject | ||
public StyledMessageUtils() { | ||
} | ||
|
||
/** | ||
* Strips all recognised control codes from the input string. | ||
* | ||
* @param input the String to be stripped | ||
* | ||
* @return a copy of the input with control codes removed | ||
*/ | ||
public String stripControlCodes(final String input) { | ||
return input.replaceAll("[" + IRCControlCodes.BOLD + CODE_CHANNEL + IRCControlCodes.FIXED | ||
+ CODE_HYPERLINK + IRCControlCodes.ITALIC + IRCControlCodes.NEGATE + CODE_NICKNAME | ||
+ CODE_SMILIE + IRCControlCodes.STOP + IRCControlCodes.UNDERLINE + "]|" | ||
+ IRCControlCodes.COLOUR_HEX + "([A-Za-z0-9]{6}(,[A-Za-z0-9]{6})?)?|" | ||
+ IRCControlCodes.COLOUR + "([0-9]{1,2}(,[0-9]{1,2})?)?", "") | ||
.replaceAll(CODE_TOOLTIP + ".*?" + CODE_TOOLTIP + "(.*?)" + CODE_TOOLTIP, "$1"); | ||
} | ||
|
||
/** | ||
* Retrieves the styled String contained within the unstyled offsets specified. That is, the | ||
* <code>from</code> and <code>to</code> arguments correspond to indexes in an unstyled version | ||
* of the <code>styled</code> string. The unstyled indices are translated to offsets within the | ||
* styled String, and the return value includes all text and control codes between those | ||
* indices. | ||
* <p> | ||
* The index translation is left-biased; that is, the indices are translated to be as far left | ||
* as they possibly can be. This means that the start of the string will include any control | ||
* codes immediately preceding the desired text, and the end will not include any trailing | ||
* codes. | ||
* <p> | ||
* This method will NOT include "internal" control codes in the output. | ||
* | ||
* @param styled The styled String to be operated on | ||
* @param from The starting index in the unstyled string | ||
* @param to The ending index in the unstyled string | ||
* | ||
* @return The corresponding text between the two indices | ||
*/ | ||
public String getStyledText(final String styled, final int from, final int to) { | ||
checkArgument(from < to, "'from' (" + from + ") must be less than 'to' (" + to + ')'); | ||
checkArgument(from >= 0, "'from' (" + from + ") must be non-negative"); | ||
|
||
final String unstyled = stripControlCodes(styled); | ||
|
||
checkArgument(to <= unstyled.length(), "'to' (" + to + ") must be less than or equal to " | ||
+ "the unstyled length (" + unstyled.length() + ')'); | ||
|
||
final String startBit = unstyled.substring(0, from); | ||
final String middleBit = unstyled.substring(from, to); | ||
final String sanitised = stripInternalControlCodes(styled); | ||
int start = from; | ||
|
||
while (!stripControlCodes(sanitised.substring(0, start)).equals(startBit)) { | ||
start++; | ||
} | ||
|
||
int end = to + start - from; | ||
|
||
while (!stripControlCodes(sanitised.substring(start, end)).equals(middleBit)) { | ||
end++; | ||
} | ||
|
||
return sanitised.substring(start, end); | ||
} | ||
|
||
/** | ||
* Strips all recognised internal control codes from the input string. | ||
* | ||
* @param input the String to be stripped | ||
* | ||
* @return a copy of the input with control codes removed | ||
*/ | ||
private String stripInternalControlCodes(final String input) { | ||
return input.replaceAll("[" + CODE_CHANNEL + CODE_HYPERLINK + CODE_NICKNAME | ||
+ CODE_SMILIE + IRCControlCodes.STOP + IRCControlCodes.UNDERLINE + ']', "") | ||
.replaceAll(CODE_TOOLTIP + ".*?" + CODE_TOOLTIP + "(.*?)" | ||
+ CODE_TOOLTIP, "$1"); | ||
} | ||
|
||
} |
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
58 changes: 58 additions & 0 deletions
58
src/test/java/com/dmdirc/ui/messages/StyledMessageUtilsTest.java
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,58 @@ | ||
/* | ||
* Copyright (c) 2006-2016 DMDirc Developers | ||
* | ||
* 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 com.dmdirc.ui.messages; | ||
|
||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class StyledMessageUtilsTest { | ||
|
||
private StyledMessageUtils styleUtils; | ||
|
||
@Before | ||
public void setUp() { | ||
styleUtils = new StyledMessageUtils(); | ||
} | ||
|
||
@Test | ||
public void testStripControlCodes1() { | ||
final String input = "This"+ (char) 2 +" is "+ (char) 17 +"a test"; | ||
|
||
final String expResult = "This is a test"; | ||
final String result = styleUtils.stripControlCodes(input); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
@Test | ||
public void testStripControlCodes2() { | ||
final String input = "This is "+ (char) 3 +"5a "+ (char) 4 +"FF0000test"; | ||
|
||
final String expResult = "This is a test"; | ||
final String result = styleUtils.stripControlCodes(input); | ||
assertEquals(expResult, result); | ||
} | ||
|
||
} |
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