JavaでStringとint[]の相互変換する方法

プログラミング
スポンサーリンク

はじめに

charAt1文字を1変数(コードポイントで)に割り当てたい。
そんな要望からStringint[]との相互変換を行ってみます。
使い方はお察し下さい。

文字列から数値配列へ

static public int[] toInteger(String text) {
	int textlength = text.length();
	for (int i = 0, j = text.length(); i < j; i++) {
		if (Character.isHighSurrogate(text.charAt(i))) {
			textlength––;
			i++;
		}
	}
	int[] out = new int[textlength];
	for (int i = 0, j = text.length(), p = 0; i < j; i++, p++) {
		int c = text.charAt(i);
		if (!Character.isHighSurrogate(text.charAt(i))) {
			out[p] = c;
		} else {
			out[p] = toCodePoint(c, text.charAt(i + 1));
			i++;
		}
	}
	return (out);
}

数値配列から文字列へ

static public String toString(int[] text) {
	StringBuilder out = new StringBuilder();
	for (int i = 0, j = text.length; i < j; i++) {
		out.append(Character.toChars(text[i]));
	}
	return (out.toString());
}

サロゲートペアの判別方法

static public int getHighSurrogate(int x) {
	x -= 0×10000;
	x >>= 10;
	x += 0xD800;
	return (x);
}
static public int getLowSurrogate(int x) {
	x -= 0×10000;
	x &= 0x3FF;
	x += 0xDC00;
	return (x);
}
static public int toCodePoint(int high, int low) {
	high -= 0xD800;
	high <<= 10;
	low -= 0xDC00;
	low |= high;
	low += 0×10000;
	return (low);
}

※ 「Character.toCodePoint」とかぶってた。

数値配列の検索

static public int indexOf(int[] in, int[] target, int offset) {
	final int ilength = in.length – target.length + 1;
	final int tlength = target.length;
	// 高速化のために頭の文字だけ配列からだしておく
	final int sento = target[0];
	if (offset >= ilength) {
		return (-1);
	}
	// 検索対象が1文字の場合
	int i;
	if (tlength == 0) {
		for (i = offset; i < ilength; i++) {
			if (in[i] == sento) {
				return (i);
			}
		}
		return (-1);
	}
	// 検索対象が複数の場合
	int j;
	for (i = offset; i < ilength; i++) {
		if (in[i] == sento) {
			i++;
			for (j = 1; j < tlength; j++, i++) {
				if (in[i] != target[j]) {
					break;
				}
			}
			if (j == tlength) {
				return (i – tlength);
			} else {
				i -= j;
			}
		}
	}
	return (-1);
}

数値配列の置換

static public int[] getReplace(int[] in, int[] target, int[] replacement) {
	if ((target.length == replacement.length) && (target.length == 1)) {
		int tgt = target[0];
		int rep = replacement[0];
		int[] out = new int[in.length];
		System.arraycopy(in, 0, out, 0, in.length);
		for (int i = in.length – 1; i >= 0; i–) {
			if (out[i] == tgt) {
				out[i] = rep;
			}
		}
		return (out);
	} else if (target.length < replacement.length) {
		return (toInteger(toString(in).replace(toString(target),
				toString(replacement))));
	}
	int[] out = new int[in.length];
	int ilength = in.length;
	int tlength = target.length;
	int rlength = replacement.length;
	int inpoint = 0, outpoint = 0, index;
	while (true) {
		index = indexOf(in, target, inpoint);
		if (index == -1) {
			System.arraycopy(in, inpoint, out, outpoint, ilength – inpoint);
			outpoint += ilength – inpoint;
			break;
		}
		System.arraycopy(in, inpoint, out, outpoint, index – inpoint);
		outpoint += index – inpoint;
		inpoint += index – inpoint;
		System.arraycopy(replacement, 0, out, outpoint, rlength);
		inpoint += tlength;
		outpoint += rlength;
	}
	if (outpoint != ilength) {
		int[] out2 = new int[outpoint];
		System.arraycopy(out, 0, out2, 0, outpoint);
		return (out2);
	}
	return (out);
}

コメント

タイトルとURLをコピーしました