Permalink
Browse files

Add BidiHack class

  • Loading branch information...
lutzky committed Oct 7, 2011
1 parent bc42d93 commit 713a9bd89547763776bb8a1c991ceb23bd6426c5
@@ -0,0 +1,49 @@
package net.lutzky.transportdroidil;

/**
* A quick hack to get around Bidi limitations in some Android versions:
* reverses number runs.
*
* @author Ohad Lutzky
* @email ohad@lutzky.net
*/
public class BidiHack {
StringBuilder forward = new StringBuilder();
StringBuilder reverse = new StringBuilder();
protected final String charsToFlip = "01234567890:/";

protected void flushReverse() {
if (reverse.length() == 0) {
return;
}

forward.append(reverse.reverse());
clear(reverse);
}

protected static void clear(StringBuilder sb) {
sb.setLength(0);
}

protected boolean needsFlipping(char c) {
return (charsToFlip.indexOf(c) != -1);
}

public String reorder(String s) {
clear(forward);
clear(reverse);

for (char c : s.toCharArray()) {
if (needsFlipping(c)) {
reverse.append(c);
}
else {
flushReverse();
forward.append(c);
}
}
flushReverse();

return forward.toString();
}
}
@@ -0,0 +1,38 @@
package net.lutzky.transportdroidil.test;

import junit.framework.TestCase;
import net.lutzky.transportdroidil.BidiHack;

public class BidiHackTest extends TestCase {
BidiHack bh;

protected void setUp() {
bh = new BidiHack();
}

public void testHebrewSanity() {
assertEquals("שלום עולם", bh.reorder("שלום עולם"));
}

public void testNumberAndText() {
assertEquals("שלום 123 עולם", bh.reorder("שלום 321 עולם"));
}
public void testDate() {
assertEquals("09/10/2011", bh.reorder("1102/01/90"));
}

public void testHour() {
assertEquals("06:35", bh.reorder("53:60"));
}

public void testMessage() {
String line1Orig = "מחיפה,גימנסיה כרמל למרכז הכרמל בתאריך 1102/01/90";
String line2Orig = "* קו 32 אגד, 50:60 52:60; 54:60; ";
String line1Fixed = "מחיפה,גימנסיה כרמל למרכז הכרמל בתאריך 09/10/2011";
String line2Fixed = "* קו 23 אגד, 06:05 06:25; 06:45; ";
assertEquals(line1Fixed, bh.reorder(line1Orig));
assertEquals(line2Fixed, bh.reorder(line2Orig));
assertEquals(line1Fixed + "\n" + line2Fixed, bh.reorder(line1Orig + "\n" + line2Orig));
}

}

0 comments on commit 713a9bd

Please sign in to comment.