RESOLVED - # 99: Change StringBuffer deleteCharAt() to integer
http://bugs.koc.se/view.php?id=99
This commit is contained in:
parent
a1acca0545
commit
e46ecc1dca
10 changed files with 99 additions and 66 deletions
|
|
@ -9,7 +9,7 @@
|
||||||
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
|
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
|
||||||
<classpathentry kind="lib" path="libs/commons-fileupload-1.2.1.jar" sourcepath="C:/Users/Ziver/Documents/Programmering/Java/libs/commons/commons-fileupload-1.2.1-sources.jar"/>
|
<classpathentry kind="lib" path="libs/commons-fileupload-1.2.1.jar" sourcepath="C:/Users/Ziver/Documents/Programmering/Java/libs/commons/commons-fileupload-1.2.1-sources.jar"/>
|
||||||
<classpathentry kind="lib" path="libs/commons-io-1.4.jar"/>
|
<classpathentry kind="lib" path="libs/commons-io-1.4.jar"/>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
|
||||||
<classpathentry kind="lib" path="libs/mysql-connector-java-5.1.14-bin.jar"/>
|
<classpathentry kind="lib" path="libs/mysql-connector-java-5.1.14-bin.jar"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
||||||
<classpathentry kind="output" path="bin"/>
|
<classpathentry kind="output" path="bin"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,11 @@ public class StringInputStream extends InputStream{
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StringInputStream(String data) {
|
||||||
|
clear();
|
||||||
|
add(data);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an estimate of the number of bytes
|
* Returns an estimate of the number of bytes
|
||||||
* that can be read (or skipped over) from this
|
* that can be read (or skipped over) from this
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ public class TorrentMetainfo {
|
||||||
|
|
||||||
|
|
||||||
private void decode(String data){
|
private void decode(String data){
|
||||||
DataNode metainfo = BEncodedParser.parse(data);
|
DataNode metainfo = BEncodedParser.read(data);
|
||||||
|
|
||||||
// Main values
|
// Main values
|
||||||
announce = metainfo.getString("announce");
|
announce = metainfo.getString("announce");
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ public class TorrentTracker {
|
||||||
private URL trackerURL;
|
private URL trackerURL;
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: incomplete
|
||||||
public void update() throws IOException {
|
public void update() throws IOException {
|
||||||
HttpClient request = HttpClient.GET();
|
HttpClient request = HttpClient.GET();
|
||||||
request.setURL( trackerURL );
|
request.setURL( trackerURL );
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,8 @@ package zutil.parser;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import zutil.struct.MutableInt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses BBCode and replaces them with the corresponding HTML.
|
* Parses BBCode and replaces them with the corresponding HTML.
|
||||||
*
|
*
|
||||||
|
|
@ -32,15 +34,6 @@ public class BBCodeParser {
|
||||||
/** Contains all the BBCodes and the corresponding HTML **/
|
/** Contains all the BBCodes and the corresponding HTML **/
|
||||||
private HashMap<String, String> bbcodes;
|
private HashMap<String, String> bbcodes;
|
||||||
|
|
||||||
public static void main(String[] args){
|
|
||||||
BBCodeParser parser = new BBCodeParser();
|
|
||||||
System.out.println(parser.parse("jshdkj [u]lol [apa]lol[/apa]"));
|
|
||||||
System.out.println(parser.parse("jshdkj [m]lol[/k] [i]lol[/i]"));
|
|
||||||
System.out.println(parser.parse("jshdkj [m"));
|
|
||||||
System.out.println(parser.parse("jshdkj [/m"));
|
|
||||||
System.out.println(parser.parse("jshdkj m]"));
|
|
||||||
System.out.println(parser.parse("jshdkj <br />"));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initiates a instance of the parser with the most used BBCodes.
|
* Initiates a instance of the parser with the most used BBCodes.
|
||||||
|
|
@ -86,20 +79,20 @@ public class BBCodeParser {
|
||||||
* @param text is a String with BBCode
|
* @param text is a String with BBCode
|
||||||
* @return a String where all BBCode has been replaced by HTML
|
* @return a String where all BBCode has been replaced by HTML
|
||||||
*/
|
*/
|
||||||
public String parse(String text){
|
public String read(String text){
|
||||||
StringBuilder out = new StringBuilder();
|
StringBuilder out = new StringBuilder();
|
||||||
StringBuilder t = new StringBuilder(text);
|
StringBuilder t = new StringBuilder(text);
|
||||||
|
|
||||||
parse(t, out, "");
|
read(new MutableInt(), t, out, "");
|
||||||
|
|
||||||
return out.toString();
|
return out.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void parse(StringBuilder text, StringBuilder out, String rootBBC){
|
private void read(MutableInt index, StringBuilder text, StringBuilder out, String rootBBC){
|
||||||
StringBuilder bbcode = null;
|
StringBuilder bbcode = null;
|
||||||
boolean closeTag = false;
|
boolean closeTag = false;
|
||||||
while(text.length() > 0){
|
while(index.i < text.length()){
|
||||||
char c = text.charAt(0);
|
char c = text.charAt(index.i);
|
||||||
if(c == '['){
|
if(c == '['){
|
||||||
bbcode = new StringBuilder();
|
bbcode = new StringBuilder();
|
||||||
}
|
}
|
||||||
|
|
@ -110,7 +103,7 @@ public class BBCodeParser {
|
||||||
String param = "";
|
String param = "";
|
||||||
switch(c){
|
switch(c){
|
||||||
case '=':
|
case '=':
|
||||||
param = parseParam(text);
|
param = parseParam(index, text);
|
||||||
case ']':
|
case ']':
|
||||||
String bbcode_cache = bbcode.toString();
|
String bbcode_cache = bbcode.toString();
|
||||||
if(closeTag){
|
if(closeTag){
|
||||||
|
|
@ -119,7 +112,7 @@ public class BBCodeParser {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String value = parseValue(text, bbcode_cache);
|
String value = parseValue(index, text, bbcode_cache);
|
||||||
if(bbcodes.containsKey( bbcode_cache )){
|
if(bbcodes.containsKey( bbcode_cache )){
|
||||||
String html = bbcodes.get( bbcode_cache );
|
String html = bbcodes.get( bbcode_cache );
|
||||||
html = html.replaceAll("%1", param);
|
html = html.replaceAll("%1", param);
|
||||||
|
|
@ -142,8 +135,8 @@ public class BBCodeParser {
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
out.append(c);
|
out.append(c);
|
||||||
if(text.length()>0)
|
|
||||||
text.deleteCharAt(0);
|
index.i++;
|
||||||
}
|
}
|
||||||
if(bbcode!=null)
|
if(bbcode!=null)
|
||||||
if(closeTag)out.append("[/").append(bbcode);
|
if(closeTag)out.append("[/").append(bbcode);
|
||||||
|
|
@ -156,15 +149,15 @@ public class BBCodeParser {
|
||||||
* @param text is the text to parse from
|
* @param text is the text to parse from
|
||||||
* @return only the parameter string
|
* @return only the parameter string
|
||||||
*/
|
*/
|
||||||
private String parseParam(StringBuilder text){
|
private String parseParam(MutableInt index, StringBuilder text){
|
||||||
StringBuilder param = new StringBuilder();
|
StringBuilder param = new StringBuilder();
|
||||||
while(text.length() > 0){
|
while(index.i < text.length()){
|
||||||
char c = text.charAt(0);
|
char c = text.charAt(index.i);
|
||||||
if(c == ']')
|
if(c == ']')
|
||||||
break;
|
break;
|
||||||
else if(c != '=')
|
else if(c != '=')
|
||||||
param.append(c);
|
param.append(c);
|
||||||
text.deleteCharAt(0);
|
index.i++;
|
||||||
}
|
}
|
||||||
return param.toString();
|
return param.toString();
|
||||||
}
|
}
|
||||||
|
|
@ -174,17 +167,17 @@ public class BBCodeParser {
|
||||||
*
|
*
|
||||||
* @param text is the text to parse the value from
|
* @param text is the text to parse the value from
|
||||||
*/
|
*/
|
||||||
private String parseValue(StringBuilder text, String bbcode){
|
private String parseValue(MutableInt index, StringBuilder text, String bbcode){
|
||||||
StringBuilder value = new StringBuilder();
|
StringBuilder value = new StringBuilder();
|
||||||
while(text.length() > 0){
|
while(index.i < text.length()){
|
||||||
char c = text.charAt(0);
|
char c = text.charAt(index.i);
|
||||||
if(c == '['){
|
if(c == '['){
|
||||||
parse(text, value, bbcode);
|
read(index, text, value, bbcode);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else if(c != ']')
|
else if(c != ']')
|
||||||
value.append(c);
|
value.append(c);
|
||||||
text.deleteCharAt(0);
|
index.i++;
|
||||||
}
|
}
|
||||||
return value.toString();
|
return value.toString();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@
|
||||||
package zutil.parser;
|
package zutil.parser;
|
||||||
|
|
||||||
import zutil.parser.DataNode.DataType;
|
import zutil.parser.DataNode.DataType;
|
||||||
|
import zutil.struct.MutableInt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* http://wiki.theory.org/BitTorrentSpecification
|
* http://wiki.theory.org/BitTorrentSpecification
|
||||||
|
|
@ -36,8 +37,8 @@ public class BEncodedParser {
|
||||||
* @param data The data to be decoded
|
* @param data The data to be decoded
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static DataNode parse(String data){
|
public static DataNode read(String data){
|
||||||
return decode_BEncoded(new StringBuffer(data));
|
return decode_BEncoded(new MutableInt(), new StringBuilder(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -47,11 +48,11 @@ public class BEncodedParser {
|
||||||
* @param index The index in data to start from
|
* @param index The index in data to start from
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private static DataNode decode_BEncoded(StringBuffer data){
|
private static DataNode decode_BEncoded(MutableInt index, StringBuilder data){
|
||||||
String tmp;
|
String tmp;
|
||||||
char c = ' ';
|
char c = ' ';
|
||||||
|
|
||||||
switch (data.charAt(0)) {
|
switch (data.charAt(index.i)) {
|
||||||
/**
|
/**
|
||||||
* Integers are prefixed with an i and terminated by an e. For
|
* Integers are prefixed with an i and terminated by an e. For
|
||||||
* example, 123 would bEcode to i123e, -3272002 would bEncode to
|
* example, 123 would bEcode to i123e, -3272002 would bEncode to
|
||||||
|
|
@ -59,9 +60,9 @@ public class BEncodedParser {
|
||||||
*/
|
*/
|
||||||
case 'i':
|
case 'i':
|
||||||
//System.out.println("Found Integer at "+index);
|
//System.out.println("Found Integer at "+index);
|
||||||
data.deleteCharAt(0);
|
index.i++;
|
||||||
tmp = data.substring(0, data.indexOf("e"));
|
tmp = data.substring(index.i, data.indexOf("e"));
|
||||||
data.delete(0, tmp.length() + 1);
|
index.i += tmp.length() + 1;
|
||||||
//System.out.println(tmp);
|
//System.out.println(tmp);
|
||||||
return new DataNode( new Long(tmp));
|
return new DataNode( new Long(tmp));
|
||||||
/**
|
/**
|
||||||
|
|
@ -73,14 +74,14 @@ public class BEncodedParser {
|
||||||
*/
|
*/
|
||||||
case 'l':
|
case 'l':
|
||||||
//System.out.println("Found List at "+index);
|
//System.out.println("Found List at "+index);
|
||||||
data.deleteCharAt(0);
|
index.i++;
|
||||||
DataNode list = new DataNode( DataType.List );
|
DataNode list = new DataNode( DataType.List );
|
||||||
c = data.charAt(0);
|
c = data.charAt(index.i);
|
||||||
while(c != 'e'){
|
while(c != 'e'){
|
||||||
list.add( decode_BEncoded(data) );
|
list.add( decode_BEncoded(index, data) );
|
||||||
c = data.charAt(0);
|
c = data.charAt(index.i);
|
||||||
}
|
}
|
||||||
data.deleteCharAt(0);
|
index.i++;
|
||||||
//MultiPrintStream.out.dump(list);
|
//MultiPrintStream.out.dump(list);
|
||||||
if(list.size() == 1) return list.get(0);
|
if(list.size() == 1) return list.get(0);
|
||||||
else return list;
|
else return list;
|
||||||
|
|
@ -92,15 +93,15 @@ public class BEncodedParser {
|
||||||
*/
|
*/
|
||||||
case 'd':
|
case 'd':
|
||||||
//System.out.println("Found Dictionary at "+index);
|
//System.out.println("Found Dictionary at "+index);
|
||||||
data.deleteCharAt(0);
|
index.i++;
|
||||||
DataNode map = new DataNode( DataType.Map );
|
DataNode map = new DataNode( DataType.Map );
|
||||||
c = data.charAt(0);
|
c = data.charAt(index.i);
|
||||||
while(c != 'e'){
|
while(c != 'e'){
|
||||||
DataNode tmp2 = decode_BEncoded(data);
|
DataNode tmp2 = decode_BEncoded(index, data);
|
||||||
map.set(tmp2.getString(), decode_BEncoded(data));
|
map.set(tmp2.getString(), decode_BEncoded(index, data));
|
||||||
c = data.charAt(0);
|
c = data.charAt(index.i);
|
||||||
}
|
}
|
||||||
data.deleteCharAt(0);
|
index.i++;
|
||||||
//MultiPrintStream.out.dump(map);
|
//MultiPrintStream.out.dump(map);
|
||||||
return map;
|
return map;
|
||||||
/**
|
/**
|
||||||
|
|
@ -110,11 +111,11 @@ public class BEncodedParser {
|
||||||
*/
|
*/
|
||||||
default:
|
default:
|
||||||
//System.out.println("Found String at "+index);
|
//System.out.println("Found String at "+index);
|
||||||
tmp = data.substring(0, data.indexOf(":"));
|
tmp = data.substring(index.i, data.indexOf(":"));
|
||||||
int length = Integer.parseInt(tmp);
|
int length = Integer.parseInt(tmp);
|
||||||
data.delete(0, tmp.length()+1);
|
index.i += tmp.length() + 1;
|
||||||
String ret = data.substring(0, length);
|
String ret = data.substring(index.i, length);
|
||||||
data.delete(0, length);
|
index.i += length;
|
||||||
//System.out.println(data.substring(i, i+length));
|
//System.out.println(data.substring(i, i+length));
|
||||||
return new DataNode( ret );
|
return new DataNode( ret );
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ package zutil.parser.json;
|
||||||
|
|
||||||
import zutil.parser.DataNode;
|
import zutil.parser.DataNode;
|
||||||
import zutil.parser.DataNode.DataType;
|
import zutil.parser.DataNode.DataType;
|
||||||
|
import zutil.struct.MutableInt;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a JSON parser class
|
* This is a JSON parser class
|
||||||
|
|
@ -30,12 +31,6 @@ import zutil.parser.DataNode.DataType;
|
||||||
* @author Ziver
|
* @author Ziver
|
||||||
*/
|
*/
|
||||||
public class JSONParser{
|
public class JSONParser{
|
||||||
/**
|
|
||||||
* A dumb class for persisting an index
|
|
||||||
*/
|
|
||||||
protected class Index{
|
|
||||||
protected int i = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts parsing
|
* Starts parsing
|
||||||
|
|
@ -44,7 +39,7 @@ public class JSONParser{
|
||||||
* @return a JSONNode object that is the root of the JSON
|
* @return a JSONNode object that is the root of the JSON
|
||||||
*/
|
*/
|
||||||
public DataNode read(String json){
|
public DataNode read(String json){
|
||||||
return parse(new Index(), new StringBuffer(json));
|
return parse(new MutableInt(), new StringBuilder(json));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -54,7 +49,7 @@ public class JSONParser{
|
||||||
* @param json
|
* @param json
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
protected static DataNode parse(Index index, StringBuffer json){
|
protected static DataNode parse(MutableInt index, StringBuilder json){
|
||||||
DataNode root = null;
|
DataNode root = null;
|
||||||
DataNode key = null;
|
DataNode key = null;
|
||||||
DataNode node = null;
|
DataNode node = null;
|
||||||
|
|
|
||||||
17
src/zutil/struct/MutableInt.java
Normal file
17
src/zutil/struct/MutableInt.java
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
package zutil.struct;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A simple class that only contains a public int. Can be used in
|
||||||
|
* recursive functions as a persistent integer.
|
||||||
|
*
|
||||||
|
* @author Ziver
|
||||||
|
*/
|
||||||
|
public class MutableInt {
|
||||||
|
public int i = 0;
|
||||||
|
|
||||||
|
public MutableInt(){}
|
||||||
|
|
||||||
|
public MutableInt(int i){
|
||||||
|
this.i = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,18 +5,13 @@ import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
|
|
||||||
import com.carrotsearch.junitbenchmarks.BenchmarkOptions;
|
|
||||||
|
|
||||||
import zutil.parser.DataNode;
|
import zutil.parser.DataNode;
|
||||||
import zutil.parser.json.JSONParser;
|
import zutil.parser.json.JSONParser;
|
||||||
|
|
||||||
|
|
||||||
public class JSONTest extends AbstractBenchmark{
|
public class JSONTest{
|
||||||
public static final int ITERATION = 200;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@BenchmarkOptions(benchmarkRounds = ITERATION)
|
|
||||||
public void JSONParser() {
|
public void JSONParser() {
|
||||||
JSONParser parser = new JSONParser();
|
JSONParser parser = new JSONParser();
|
||||||
|
|
||||||
|
|
|
||||||
27
src/zutil/test/ParserTest.java
Normal file
27
src/zutil/test/ParserTest.java
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
package zutil.test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import zutil.parser.BBCodeParser;
|
||||||
|
|
||||||
|
|
||||||
|
public class ParserTest{
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void BBCodeParser() {
|
||||||
|
BBCodeParser parser = new BBCodeParser();
|
||||||
|
|
||||||
|
assertEquals("1234", parser.read("1234"));
|
||||||
|
assertEquals("<i>1234</i>", parser.read("[i]1234[/i]"));
|
||||||
|
assertEquals("[apa]lol[/apa]", parser.read("[apa]lol[/apa]"));
|
||||||
|
assertEquals("jshdkj <u>lol [apa]lol[/apa]</u>", parser.read("jshdkj [u]lol [apa]lol[/apa]"));
|
||||||
|
//assertEquals("jshdkj [m]lol[/k] <i>lol</i>", parser.read("jshdkj [m]lol[/k] [i]lol[/i]"));
|
||||||
|
assertEquals("jshdkj [m", parser.read("jshdkj [m"));
|
||||||
|
assertEquals("jshdkj [/m", parser.read("jshdkj [/m"));
|
||||||
|
assertEquals("jshdkj m]", parser.read("jshdkj m]"));
|
||||||
|
assertEquals("jshdkj <br />", parser.read("jshdkj <br />"));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue