Created
May 24, 2014 11:17
-
-
Save pocket7878/87a2f35c2c9b6b91342b 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
package jp.dip.poketo7878; | |
/** | |
* Created by masato on 2014/05/24. | |
*/ | |
public class StringEditor { | |
private String str; | |
public Edit[] edits; | |
public StringEditor(String sr) { | |
this.str = sr; | |
this.edits = new Edit[this.str.length()]; | |
for(int i = 0; i < this.str.length(); i++) { | |
this.edits[i] = new Edit(this.str.substring(i,i+1)); | |
} | |
} | |
/* | |
* 全てのインデックスは文字と文字の間の位置を指す | |
*/ | |
//idx文字目の後ろに追加 | |
public void insert(String s, int idx) { | |
this.edits[idx].insert += s; | |
} | |
//from文字目からto文字目をstringで置換 | |
public void replace(String s, int from, int to) { | |
//文字列のほうが長い場合 | |
if(s.length() > Math.abs(to - from + 1)) { | |
//まずは範囲の文字を置換 | |
for(int i = from; i <= to; i++) { | |
this.edits[i].rep = (s.charAt(i - from) + ""); | |
} | |
//そして範囲の最後の文字に残りの文字をappendすることにする | |
this.edits[to].append += s.substring(Math.abs(to - from) + 1); | |
} else { | |
//文字列が同じ長さか、文字列のほうが身近い場合 | |
//まずは文字列の長さ分置換していく | |
for(int i = 0; i < s.length(); i++) { | |
this.edits[i + from].rep = (s.charAt(i) + ""); | |
} | |
//残りの範囲に削除マークを付ける | |
for(int j = 1; j <= (Math.abs(to - from) - s.length()); j++) { | |
this.edits[to + 1].deleted = true; | |
} | |
} | |
} | |
//idx文字目の後に追加 | |
public void append(String s, int idx) { | |
this.edits[idx].append = s + this.edits[idx].append; | |
} | |
//idx文字目を削除 | |
public void delete(int idx) { | |
this.edits[idx].deleted = true; | |
} | |
//from文字目からto文字目を削除 | |
public void delete(int from, int to) { | |
for(int i = from; i <= to; i++) { | |
this.edits[i].deleted = true; | |
} | |
} | |
public String toString() { | |
String s = ""; | |
for(int i = 0; i < this.str.length(); i++) { | |
Edit e = edits[i]; | |
s += e.toString(); | |
} | |
return s; | |
} | |
public static void main(String[] args) { | |
String base = "0 1 2 3 4 5 6 7 8"; | |
StringEditor se = new StringEditor(base); | |
se.replace("vi", 6,6); | |
System.out.println(se.toString()); | |
for(Edit e: se.edits) { | |
System.out.println(e.infoString()); | |
} | |
se.append("Hoge",6); | |
System.out.println(se.toString()); | |
for(Edit e: se.edits) { | |
System.out.println(e.infoString()); | |
} | |
se.insert("Moge",6); | |
System.out.println(se.toString()); | |
for(Edit e: se.edits) { | |
System.out.println(e.infoString()); | |
} | |
} | |
} | |
class Edit { | |
private String base; | |
public boolean deleted; | |
public String append = ""; | |
public String insert = ""; | |
public String rep = null; | |
public Edit(String base) { | |
this.base = base; | |
} | |
@Override | |
public String toString() { | |
String b = null; | |
if(deleted) { | |
b = ""; | |
} else if(rep != null) { | |
b = rep; | |
} else { | |
b = base; | |
} | |
return (insert + b + append); | |
} | |
public String infoString() { | |
String b = null; | |
if(deleted) { | |
b = ""; | |
} else if(rep != null) { | |
b = rep; | |
} else { | |
b = base; | |
} | |
return "{insert: " + insert + ", b: " + b + ", append: " + append + "}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment