Permalink
Browse files

Merge branch 'optimizations' of github.com:twitter/twitter-text-java …

…into optimizations
  • Loading branch information...
2 parents 5f4a84a + a97b85c commit 59aa00fc17dc7cd7c781aae92afffdc36d15eb4c @spullara spullara committed Oct 22, 2011
@@ -105,7 +105,7 @@ public String autoLinkUsernamesAndLists(String text) {
matcher = Regex.AUTO_LINK_USERNAMES_OR_LISTS.matcher(chunk);
while (matcher.find()) {
if (matcher.group(Regex.AUTO_LINK_USERNAME_OR_LISTS_GROUP_LIST) == null ||
- matcher.group(Regex.AUTO_LINK_USERNAME_OR_LISTS_GROUP_LIST).equals("")) {
+ matcher.group(Regex.AUTO_LINK_USERNAME_OR_LISTS_GROUP_LIST).isEmpty()) {
// Username only
if (!Regex.SCREEN_NAME_MATCH_END.matcher(text.substring(matcher.end())).find()) {
@@ -355,34 +355,29 @@ public void setNoFollow(boolean noFollow) {
}
// The default String split is horribly inefficient
- private static Iterable<String> split(final String s, final String d) {
+ protected static Iterable<String> split(final String s, final String d) {
List<String> strings = new ArrayList<String>();
int length = s.length();
int current = 0;
- boolean onemore = false;
- while (current < length || onemore) {
- int start = current;
- int i = -1;
- for (int j = 0; j < d.length(); j++) {
- int index = s.indexOf(d.charAt(j), start);
- if (i == -1) {
- i = index;
- } else {
- if (index != -1 && index < i) {
- i = index;
- }
+ while (current < length) {
+ int minIndex = Integer.MAX_VALUE;
+ for (char c : d.toCharArray()) {
+ int index = s.indexOf(c, current);
+ if (index != -1 && index < minIndex) {
+ minIndex = index;
}
}
- if (i == -1) {
+ if (minIndex == Integer.MAX_VALUE) {
+ // s doesn't contain any char in d
+ strings.add(s.substring(current));
current = length;
- onemore = false;
- strings.add(s.substring(start));
} else {
- current = i + 1;
+ strings.add(s.substring(current, minIndex));
+ current = minIndex + 1;
if (current == length) {
- onemore = true;
+ // last char in s is in d.
+ strings.add("");
}
- strings.add(s.substring(start, i));
}
}
return strings;
@@ -1,6 +1,8 @@
package com.twitter;
+import java.util.Arrays;
+
import junit.framework.TestCase;
public class AutolinkTest extends TestCase {
@@ -56,4 +58,18 @@ public void testWithAngleBrackets() {
protected void assertAutolink(String expected, String linked) {
assertEquals("Autolinked text should equal the input", expected, linked);
}
+
+ public void testSplit() {
+ String text = "Url: <a href=\"http://example.com/$ABC\">http://example.com/$ABC</a>";
+ assertEquals(Arrays.asList(text.split("[<>]", -1)), Autolink.split(text, "<>"));
+
+ text = "";
+ assertFalse(Autolink.split(text, "<>").iterator().hasNext());
+
+ text = "there is no bracket in this text.";
+ assertEquals(Arrays.asList(text.split("[<>]", -1)), Autolink.split(text, "<>"));
+
+ text = "><><><><";
+ assertEquals(Arrays.asList(text.split("[<>]", -1)), Autolink.split(text, "<>"));
+ }
}
@@ -6,7 +6,7 @@
/**
* Micro benchmark for discovering hotspots in our autolinker.
*/
-public class Benchmark extends ConformanceBase {
+public class Benchmark extends ConformanceTest {
private static final int AUTO_LINK_TESTS = 10000;
private static final int ITERATIONS = 10;
@@ -1,49 +0,0 @@
-package com.twitter;
-
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.util.List;
-import java.util.Map;
-
-import junit.framework.TestCase;
-import org.ho.yaml.Yaml;
-
-/**
- * Base class for the Conformance test and the Benchmark test
- */
-public class ConformanceBase extends TestCase {
- private static final String CONFORMANCE_DIR_PROPERTY = "conformance.dir";
- protected static final String KEY_DESCRIPTION = "description";
- protected static final String KEY_INPUT = "text";
- protected static final String KEY_EXPECTED_OUTPUT = "expected";
- protected static final String KEY_HIGHLIGHT_HITS = "hits";
- protected File conformanceDir;
- protected Extractor extractor = new Extractor();
- protected Autolink linker = new Autolink();
- protected HitHighlighter hitHighlighter = new HitHighlighter();
-
- public void setUp() {
- assertNotNull("Missing required system property: " + CONFORMANCE_DIR_PROPERTY, System.getProperty(
- CONFORMANCE_DIR_PROPERTY));
- conformanceDir = new File(System.getProperty(CONFORMANCE_DIR_PROPERTY));
- assertTrue("Conformance directory " + conformanceDir + " is not a directory.", conformanceDir.isDirectory());
-
- assertNotNull("No extractor configured", extractor);
- assertNotNull("No autolinker configured", linker);
- linker.setNoFollow(false);
- }
-
- protected void autolink(List<Map> testCases) {
- for (Map testCase : testCases) {
- assertEquals((String)testCase.get(KEY_DESCRIPTION),
- (String)testCase.get(KEY_EXPECTED_OUTPUT),
- linker.autoLink((String) testCase.get(KEY_INPUT)));
- }
- }
-
- protected List loadConformanceData(File yamlFile, String testType) throws FileNotFoundException {
- Map fullConfig = (Map) Yaml.load(yamlFile);
- Map testConfig = (Map)fullConfig.get("tests");
- return (List)testConfig.get(testType);
- }
-}
@@ -10,7 +10,17 @@
import junit.framework.TestCase;
import org.ho.yaml.Yaml;
-public class ConformanceTest extends ConformanceBase {
+public class ConformanceTest extends TestCase {
+
+ private static final String CONFORMANCE_DIR_PROPERTY = "conformance.dir";
+ protected static final String KEY_DESCRIPTION = "description";
+ protected static final String KEY_INPUT = "text";
+ protected static final String KEY_EXPECTED_OUTPUT = "expected";
+ protected static final String KEY_HIGHLIGHT_HITS = "hits";
+ protected File conformanceDir;
+ protected Extractor extractor = new Extractor();
+ protected Autolink linker = new Autolink();
+ protected HitHighlighter hitHighlighter = new HitHighlighter();
public void testMentionsExtractor() throws Exception {
File yamlFile = new File(conformanceDir, "extract.yml");
@@ -128,4 +138,28 @@ public void testPlainTextHitHighlighting() throws Exception {
}
}
+ public void setUp() {
+ assertNotNull("Missing required system property: " + CONFORMANCE_DIR_PROPERTY, System.getProperty(
+ CONFORMANCE_DIR_PROPERTY));
+ conformanceDir = new File(System.getProperty(CONFORMANCE_DIR_PROPERTY));
+ assertTrue("Conformance directory " + conformanceDir + " is not a directory.", conformanceDir.isDirectory());
+
+ assertNotNull("No extractor configured", extractor);
+ assertNotNull("No autolinker configured", linker);
+ linker.setNoFollow(false);
+ }
+
+ protected void autolink(List<Map> testCases) {
+ for (Map testCase : testCases) {
+ assertEquals((String)testCase.get(KEY_DESCRIPTION),
+ (String)testCase.get(KEY_EXPECTED_OUTPUT),
+ linker.autoLink((String) testCase.get(KEY_INPUT)));
+ }
+ }
+
+ protected List loadConformanceData(File yamlFile, String testType) throws FileNotFoundException {
+ Map fullConfig = (Map) Yaml.load(yamlFile);
+ Map testConfig = (Map)fullConfig.get("tests");
+ return (List)testConfig.get(testType);
+ }
}

0 comments on commit 59aa00f

Please sign in to comment.