Permalink
Browse files

Minor bug fixes with utility functions, and documentation updates.

  • Loading branch information...
1 parent 0b40f00 commit 9bd620d012a21d6302a54bb9526a2402a9fc5c17 @LadyCailin LadyCailin committed Sep 11, 2012
View
@@ -9,9 +9,10 @@ this means that you cannot submit code that adds Objects, for instance, and keep
the copyright on it, you must release that to me.
This is because I intend for MethodScript to live past Minecraft, and I do not
-want to be burdened by license restrictions. In all likelyhood, I will always
+want to be burdened by license restrictions. In all likelihood, I will always
release the core under MIT, however, I am not willing to make any promises,
so please keep that in mind, and if me selling the software without crediting
you in the future makes you uncomfortable, please do not submit code to the core.
-Code that relates to Minecraft itself will always be MIT licensed,
+Code that relates to Minecraft itself will always be MIT licensed, and your
+Minecraft related submissions are always released under MIT.
@@ -47,8 +47,9 @@
int size = Math.abs(start - finish) + 1;
Object [] newArray = new Object[size];
if(start <= finish){
+ int counter = 0;
for(int i = start; i <= finish; i++){
- newArray[i] = array[i];
+ newArray[counter++] = array[i];
}
} else {
int counter = 0;
@@ -73,12 +74,14 @@
int size = Math.abs(start - finish) + 1;
char [] newArray = new char[size];
if(start <= finish){
+ int counter = 0;
for(int i = start; i <= finish; i++){
- newArray[i] = array[i];
+ newArray[counter++] = array[i];
}
} else {
- for(int i = finish; i >= start; i++){
- newArray[i] = array[i];
+ int counter = 0;
+ for(int i = start; i >= finish; i--){
+ newArray[counter++] = array[i];
}
}
return newArray;
@@ -97,12 +100,14 @@
int size = Math.abs(start - finish) + 1;
byte [] newArray = new byte[size];
if(start <= finish){
+ int counter = 0;
for(int i = start; i <= finish; i++){
- newArray[i] = array[i];
+ newArray[counter++] = array[i];
}
} else {
- for(int i = finish; i >= start; i++){
- newArray[i] = array[i];
+ int counter = 0;
+ for(int i = start; i >= finish; i--){
+ newArray[counter++] = array[i];
}
}
return newArray;
@@ -121,12 +126,14 @@
int size = Math.abs(start - finish) + 1;
short [] newArray = new short[size];
if(start <= finish){
+ int counter = 0;
for(int i = start; i <= finish; i++){
- newArray[i] = array[i];
+ newArray[counter++] = array[i];
}
} else {
- for(int i = finish; i >= start; i++){
- newArray[i] = array[i];
+ int counter = 0;
+ for(int i = start; i >= finish; i--){
+ newArray[counter++] = array[i];
}
}
return newArray;
@@ -145,12 +152,14 @@
int size = Math.abs(start - finish) + 1;
int [] newArray = new int[size];
if(start <= finish){
+ int counter = 0;
for(int i = start; i <= finish; i++){
- newArray[i] = array[i];
+ newArray[counter++] = array[i];
}
} else {
- for(int i = finish; i >= start; i++){
- newArray[i] = array[i];
+ int counter = 0;
+ for(int i = start; i >= finish; i--){
+ newArray[counter++] = array[i];
}
}
return newArray;
@@ -169,12 +178,14 @@
int size = Math.abs(start - finish) + 1;
long [] newArray = new long[size];
if(start <= finish){
+ int counter = 0;
for(int i = start; i <= finish; i++){
- newArray[i] = array[i];
+ newArray[counter++] = array[i];
}
} else {
- for(int i = finish; i >= start; i++){
- newArray[i] = array[i];
+ int counter = 0;
+ for(int i = start; i >= finish; i--){
+ newArray[counter++] = array[i];
}
}
return newArray;
@@ -193,12 +204,14 @@
int size = Math.abs(start - finish) + 1;
float [] newArray = new float[size];
if(start <= finish){
+ int counter = 0;
for(int i = start; i <= finish; i++){
- newArray[i] = array[i];
+ newArray[counter++] = array[i];
}
} else {
- for(int i = finish; i >= start; i++){
- newArray[i] = array[i];
+ int counter = 0;
+ for(int i = start; i >= finish; i--){
+ newArray[counter++] = array[i];
}
}
return newArray;
@@ -217,12 +230,14 @@
int size = Math.abs(start - finish) + 1;
double [] newArray = new double[size];
if(start <= finish){
+ int counter = 0;
for(int i = start; i <= finish; i++){
- newArray[i] = array[i];
+ newArray[counter++] = array[i];
}
} else {
- for(int i = finish; i >= start; i++){
- newArray[i] = array[i];
+ int counter = 0;
+ for(int i = start; i >= finish; i--){
+ newArray[counter++] = array[i];
}
}
return newArray;
@@ -241,12 +256,14 @@
int size = Math.abs(start - finish) + 1;
boolean [] newArray = new boolean[size];
if(start <= finish){
+ int counter = 0;
for(int i = start; i <= finish; i++){
- newArray[i] = array[i];
+ newArray[counter++] = array[i];
}
} else {
- for(int i = finish; i >= start; i++){
- newArray[i] = array[i];
+ int counter = 0;
+ for(int i = start; i >= finish; i--){
+ newArray[counter++] = array[i];
}
}
return newArray;
@@ -0,0 +1,108 @@
+package com.laytonsmith.PureUtilities;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * Represents a javadoc style comment block, at its lowest level.
+ * @author lsmith
+ */
+public class SmartComment {
+
+ private static final Pattern ANNOTATION = Pattern.compile("@[a-zA-Z][a-zA-Z0-9]*");
+ private String raw;
+ private String body;
+ private Map<String, List<String>> annotations = new HashMap<String, List<String>>();
+ /**
+ * Creates a new smart comment. A smart comment is defined as a comment block that starts
+ * with slash star star, and ends with star slash. Newlines and duplicate whitespace are ignored
+ * in the comment, and if a line starts with a star, it is also ignored.
+ * @param comment
+ */
+ public SmartComment(String comment){
+ comment = comment.trim();
+ if(comment.startsWith("/**")){
+ comment = comment.substring(3);
+ }
+ if(comment.endsWith("*/")){
+ comment = comment.substring(0, comment.length() - 2);
+ }
+ String [] lines = comment.split("\n|\r\n|\n\r");
+ StringBuilder b = new StringBuilder();
+ for(String line : lines){
+ line = line.replaceAll("\\s+", " ");
+ if(line.startsWith(" *")){
+ line = line.substring(2);
+ } else if(line.startsWith("*")){
+ line = line.substring(1);
+ }
+ b.append(" ").append(line);
+ }
+ raw = b.toString().replaceAll("\\s+", " ").trim();
+ String [] words = raw.split(" ");
+ StringBuilder buffer = new StringBuilder();
+ String lastAnnotation = null;
+ for(String word : words){
+ if(ANNOTATION.matcher(word).matches()){
+ processBuffer(lastAnnotation, buffer.toString());
+ lastAnnotation = word;
+ buffer = new StringBuilder();
+ } else {
+ buffer.append(" ").append(word);
+ }
+ }
+ processBuffer(lastAnnotation, buffer.toString());
+ }
+
+ private void processBuffer(String lastAnnotation, String buffer){
+ if(lastAnnotation == null){
+ body = buffer.trim();
+ } else {
+ addAnnotation(lastAnnotation, buffer.trim());
+ }
+ }
+
+ private void addAnnotation(String name, String value){
+ if(!annotations.containsKey(name)){
+ annotations.put(name, new ArrayList<String>());
+ }
+ annotations.get(name).add(value);
+ }
+
+ /**
+ * Gets the body of the comment block.
+ * @return
+ */
+ public String getBody(){
+ return body;
+ }
+
+ /**
+ * Gets a list of annotation values for the comment block.
+ * @param annotation
+ * @return
+ */
+ public List<String> getAnnotations(String annotation){
+ if(!annotation.startsWith("@")){
+ annotation = "@" + annotation;
+ }
+ return new ArrayList<String>(annotations.get(annotation));
+ }
+
+// public static void main(String [] args){
+// SmartComment s = new SmartComment(
+// "/**\n"
+// + " * This is a comment block\n"
+// + " * @param type this is the value of a param\n"
+// + " * @empty\n"
+// + " * @empty\n"
+// + " * @param second param value weee!\n"
+// + " */\n");
+// System.out.println(s.getBody());
+// System.out.println(s.getAnnotations("empty"));
+// System.out.println(s.getAnnotations("param"));
+// }
+}
@@ -196,4 +196,18 @@ public static String trimRight(String str) {
str = str.substring(0, len);
return str;
}
+
+ /**
+ * Works like String.split(), but trims each of the entries also.
+ * @param string
+ * @param regex
+ * @return
+ */
+ public static String [] trimSplit(String string, String regex){
+ String [] split = string.split(regex);
+ for(int i = 0; i < split.length; i++){
+ split[i] = split[i].trim();
+ }
+ return split;
+ }
}
@@ -85,6 +85,16 @@ public boolean canOptimize() {
public Construct optimize(Target t, Construct... args) throws ConfigCompileException {
return exec(t, null, args);
}
+
+ @Override
+ public ExampleScript[] examples() throws ConfigCompileException {
+ return new ExampleScript[]{
+ new ExampleScript("Basic usage", "rot13('string')"),
+ new ExampleScript("Basic usage", "rot13('fgevat')"),
+ };
+ }
+
+
}
@api
@@ -139,6 +149,14 @@ public boolean canOptimize() {
public Construct optimize(Target t, Construct... args) throws ConfigCompileException {
return exec(t, null, args);
}
+
+ @Override
+ public ExampleScript[] examples() throws ConfigCompileException {
+ return new ExampleScript[]{
+ new ExampleScript("Basic usage", "md5('string')"),
+ new ExampleScript("Basic usage", "md5('String')"),
+ };
+ }
}
@api
@@ -192,6 +210,14 @@ public boolean canOptimize() {
public Construct optimize(Target t, Construct... args) throws ConfigCompileException {
return exec(t, null, args);
}
+
+ @Override
+ public ExampleScript[] examples() throws ConfigCompileException {
+ return new ExampleScript[]{
+ new ExampleScript("Basic usage", "sha1('string')"),
+ new ExampleScript("Basic usage", "sha1('String')"),
+ };
+ }
}
@api public static class sha256 extends AbstractFunction{
@@ -244,6 +270,14 @@ public boolean canOptimize() {
public Construct optimize(Target t, Construct... args) throws ConfigCompileException {
return exec(t, null, args);
}
+
+ @Override
+ public ExampleScript[] examples() throws ConfigCompileException {
+ return new ExampleScript[]{
+ new ExampleScript("Basic usage", "sha256('string')"),
+ new ExampleScript("Basic usage", "sha256('String')"),
+ };
+ }
}
@@ -288,6 +322,14 @@ public String docs() {
public CHVersion since() {
return CHVersion.V3_3_1;
}
+
+ @Override
+ public ExampleScript[] examples() throws ConfigCompileException {
+ return new ExampleScript[]{
+ new ExampleScript("Basic usage", "bcrypt('string')"),
+ new ExampleScript("Basic usage", "bcrypt('String')"),
+ };
+ }
}
@@ -326,6 +368,15 @@ public String docs() {
public CHVersion since() {
return CHVersion.V3_3_1;
}
+
+ @Override
+ public ExampleScript[] examples() throws ConfigCompileException {
+ return new ExampleScript[]{
+ new ExampleScript("Basic usage", "assign(@plain, 'plaintext')\nassign(@hash, bcrypt(@plain))\n"
+ + "msg(if(check_bcrypt(@plain, @hash), 'They match!', 'They do not match!'))\n"
+ + "msg(if(check_bcrypt('notTheRightPassword', @hash), 'They match!', 'They do not match!'))"),
+ };
+ }
}
Oops, something went wrong.

0 comments on commit 9bd620d

Please sign in to comment.