StrUtils.lastChar
diff --git a/jena-base/src/main/java/org/apache/jena/atlas/lib/StrUtils.java b/jena-base/src/main/java/org/apache/jena/atlas/lib/StrUtils.java
index 4a22344..45828d4 100644
--- a/jena-base/src/main/java/org/apache/jena/atlas/lib/StrUtils.java
+++ b/jena-base/src/main/java/org/apache/jena/atlas/lib/StrUtils.java
@@ -138,6 +138,15 @@
return x.isEmpty() ? x : x.substring(0, x.length() - 1) ;
}
+ /**
+ * Return the last character of a string,
+ * return char zero if the string is zero
+ * length.
+ */
+ public static char lastChar(String x) {
+ return x.isEmpty() ? 0 : x.charAt(x.length()-1);
+ }
+
public static String noNewlineEnding(String x) {
while (x.endsWith("\n") || x.endsWith("\r"))
x = StrUtils.chop(x) ;
diff --git a/jena-base/src/test/java/org/apache/jena/atlas/lib/TestStrUtils.java b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestStrUtils.java
index 41dd980..0b561b8 100644
--- a/jena-base/src/test/java/org/apache/jena/atlas/lib/TestStrUtils.java
+++ b/jena-base/src/test/java/org/apache/jena/atlas/lib/TestStrUtils.java
@@ -30,11 +30,11 @@
static char marker = '_' ;
static char esc[] = { ' ' , '_' } ;
- static void test(String x) {
- test(x, null);
+ static void testEnc(String x) {
+ testEnc(x, null);
}
- static void test(String x, String z) {
+ static void testEnc(String x, String z) {
String y = StrUtils.encodeHex(x, marker, esc);
if ( z != null )
assertEquals(z, y);
@@ -48,44 +48,42 @@
assertEquals(expected, x2);
}
- @Test public void enc01() { test("abc") ; }
-
- @Test public void enc02() { test("") ; }
-
- @Test public void enc03() { test("_", "_5F" ) ; }
-
- @Test public void enc04() { test(" ", "_20" ) ; }
-
- @Test public void enc05() { test("_ _", "_5F_20_5F" ) ; }
-
- @Test public void enc06() { test("_5F", "_5F5F" ) ; }
-
- @Test public void enc07() { test("_2") ; }
-
- @Test public void enc08() { test("AB_CD", "AB_5FCD") ; }
+ @Test public void enc01() { testEnc("abc") ; }
+ @Test public void enc02() { testEnc("") ; }
+ @Test public void enc03() { testEnc("_", "_5F" ) ; }
+ @Test public void enc04() { testEnc(" ", "_20" ) ; }
+ @Test public void enc05() { testEnc("_ _", "_5F_20_5F" ) ; }
+ @Test public void enc06() { testEnc("_5F", "_5F5F" ) ; }
+ @Test public void enc07() { testEnc("_2") ; }
+ @Test public void enc08() { testEnc("AB_CD", "AB_5FCD") ; }
// JENA-1890: Multibyte characters before the "_"
// 사용_설명서 (Korean: "User's Guide")
- @Test public void enc09() { test("\uC0AC\uC6A9_\uC124\uBA85\uC11C"); }
+ @Test public void enc09() { testEnc("\uC0AC\uC6A9_\uC124\uBA85\uC11C"); }
// Same string, but using the glyphs for the codepoints, not the \ u value. Same string after Java parsing.
- @Test public void enc09a() { test("사용_설명서"); }
+ @Test public void enc09a() { testEnc("사용_설명서"); }
// The decode code works more generally than the encoder.
// This tests the decode of the UTF=-8 byte encoding of 사용_설명서
// Note "_5F" which is "_" encoded.
@Test public void enc10() { testDecode("_EC_82_AC_EC_9A_A9_5F_EC_84_A4_EB_AA_85_EC_84_9C", "사용_설명서"); }
-
@Test public void enc11() { testDecode("_41", "A"); }
@Test(expected=AtlasException.class) public void enc20() { testDecode("_4", null); }
-
@Test(expected=AtlasException.class) public void enc21() { testDecode("_", null); }
-
@Test(expected=AtlasException.class) public void enc22() { testDecode("_X1", null); }
-
@Test(expected=AtlasException.class) public void enc23() { testDecode("_1X", null); }
+ @Test public void lastChar01() { testLastChar("abc", 'c'); }
+ @Test public void lastChar02() { testLastChar(".", '.'); }
+ @Test public void lastChar03() { testLastChar("", (char)0); }
+
+ private static void testLastChar(String x, char expectedLastChar) {
+ char lastChar = StrUtils.lastChar(x);
+ assertEquals(expectedLastChar, lastChar);
+ }
+
@Test public void prefix_ignorecase_1() {
boolean b = StrUtils.strStartsWithIgnoreCase("foobar", "FOO");
assertTrue(b);
@@ -113,6 +111,4 @@
boolean b = StrUtils.strEndsWithIgnoreCase("bar", "foobar");
assertFalse(b);
}
-
-
}