Skip to content

Commit

Permalink
Minor clarifications on the purpose of the different substring repres…
Browse files Browse the repository at this point in the history
…entations, and some other minor renaming of things
  • Loading branch information
klette committed Jan 25, 2015
1 parent b763b32 commit e0ec27a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
29 changes: 18 additions & 11 deletions src/main/java/us/klette/constantstring/CStringSub.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@
import javax.annotation.Nonnull;

/**
* Representation of a substring operation.
* Representation of a substring operation that is unbounded in length.
*
* This is a separate form of {@link us.klette.constantstring.CStringSubRange}
* since we don't know the length of the computed value in the child graph.
*
* Encoding this operation as a separate node type simplifies the implementation,
* by not having to encode the size as unknown (using 0,-1, null or similar) as
* the "length", and dealing with that in in the toString()-method.
*
* @author Kristian Klette (klette@klette.us)
*/
Expand All @@ -46,25 +53,25 @@ class CStringSub implements CString {
/**
* The index from which the text is kept.
*/
private final int idx;
private final int beginIndex;

/**
* Creates a new substring operation on the given value.
*
* @param val The value used as the child node for the operation.
* @param index The index from which the text is kept.
* @param value The value used as the child node for the operation.
* @param beginIndex The index from which the text is kept.
*/
public CStringSub(@Nonnull final CString val, final int index) {
this.value = val;
this.idx = index;
public CStringSub(@Nonnull final CString value, final int beginIndex) {
this.value = value;
this.beginIndex = beginIndex;
}

@Override
public final String toString() {
final String childEval = this.value.toString();
final int length = childEval.length();
return this.idx > length - 1
final String valueAsString = this.value.toString();
final int length = valueAsString.length();
return this.beginIndex > length - 1
? ""
: childEval.substring(this.idx);
: valueAsString.substring(this.beginIndex);
}
}
22 changes: 11 additions & 11 deletions src/main/java/us/klette/constantstring/CStringSubRange.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import javax.annotation.Nonnull;

/**
* Representation of a substring operation.
* Representation of a bounded substring operation.
*
* @author Kristian Klette (klette@klette.us)
*/
Expand All @@ -47,32 +47,32 @@ class CStringSubRange implements CString {
/**
* The index from which the text is kept.
*/
private final int idx;
private final int beginIndex;

/**
* The number of chars after {@link #idx} to include.
* The number of chars after {@link #beginIndex} to include.
*/
private final int end;
private final int length;

/**
* Creates a new substring operation on the given value.
*
* @param val The value used as the child node for the operation.
* @param index The index from which the text is kept.
* @param end The end index of the substring
* @param beginIndex The index from which the text is kept.
* @param length The length index of the substring
*/
public CStringSubRange(@Nonnull final CString val, final int index, final int end) {
public CStringSubRange(@Nonnull final CString val, final int beginIndex, final int length) {
this.value = val;
this.idx = index;
this.end = end;
this.beginIndex = beginIndex;
this.length = length;
}

@Override
public final String toString() {
final String childEval = this.value.toString();
final int length = childEval.length();
return this.idx > length - 1
return this.beginIndex > length - 1
? ""
: childEval.substring(this.idx, Math.min(end, length));
: childEval.substring(this.beginIndex, Math.min(this.length, length));
}
}

0 comments on commit e0ec27a

Please sign in to comment.