Created
November 2, 2017 04:35
-
-
Save kazuhito-m/a0ad277d8de05ec0fb8cd4b89621503e to your computer and use it in GitHub Desktop.
サロゲートペアに関するいろいろ確認
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
import org.junit.Test; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.junit.Assert.assertThat; | |
public class SurrogatePairsTest { | |
@Test | |
public void 文字コード指定でサロゲートペア文字列を作成する() { | |
char[] surrogatePairChars = new char[]{0xd867, 0xde3d}; // サカナ偏に花 | |
String surrogatePair = String.valueOf(surrogatePairChars); | |
// System.out.println(surrogatePair); | |
assertThat(surrogatePair, is("𩸽")); | |
surrogatePairChars = new char[]{0xd801, 0xdc37}; // デザレット文字 Aを逆にしてひねったようなヤツ | |
surrogatePair = String.valueOf(surrogatePairChars); | |
// System.out.println(surrogatePair); | |
} | |
@Test | |
public void コードポイントからサロゲートペア文字を作成する() { | |
String surrogatePair = "𩸽"; | |
int codePoint = surrogatePair.codePointAt(0); | |
assertThat(codePoint, is(0x29e3d)); | |
String restoration = new String(Character.toChars(codePoint)); | |
assertThat(restoration, is(surrogatePair)); | |
} | |
private boolean hasSurrogatePair(String text) { | |
return text.length() != text.codePoints().toArray().length; | |
} | |
@Test | |
public void 文字列中にサロゲートペア文字列が含まれているか確認する() { | |
boolean hasSurrogatePair = hasSurrogatePair("abc𩸽def"); | |
assertThat(hasSurrogatePair, is(true)); | |
hasSurrogatePair = hasSurrogatePair("abc三浦def"); | |
assertThat(hasSurrogatePair, is(false)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment