Skip to content

Commit

Permalink
feedback address
Browse files Browse the repository at this point in the history
  • Loading branch information
xueyumusic committed Apr 10, 2019
1 parent a6f7ab4 commit 7d3474a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,14 @@ public static String repeat(String s, int count)

/**
* Returns the string left-padded with the string pad to a length of len characters.
* If str is longer than len, the return value is shortened to len characters.
* If str is longer than len, the return value is shortened to len characters.
* Lpad and rpad functions are migrated from flink's scala function with minor refactor
* https://github.com/apache/flink/blob/master/flink-table/flink-table-planner/src/main/scala/org/apache/flink/table/runtime/functions/ScalarFunctions.scala
*
* @param base The base string to be padded
* @param len The length of padded string
* @param pad The pad string
* @return the string left-padded with pad to a lenght of len
* @return the string left-padded with pad to a length of len
*/
public static String lpad(String base, Integer len, String pad)
{
Expand All @@ -430,32 +432,23 @@ public static String lpad(String base, Integer len, String pad)
}

char[] data = new char[len];
char[] baseChars = base.toCharArray();
char[] padChars = pad.toCharArray();

// The length of the padding needed
int pos = Math.max(len - base.length(), 0);

// Copy the padding
int i = 0;
while (i < pos) {
int j = 0;
while (j < pad.length() && j < pos - i) {
data[i + j] = padChars[j];
j += 1;
for (int i = 0; i < pos; i += pad.length()) {
for (int j = 0; j < pad.length() && j < pos - i; j++) {
data[i + j] = pad.charAt(j);
}
i += pad.length();
}

// Copy the base
i = 0;
while (pos + i < len && i < base.length()) {
data[pos + i] = baseChars[i];
i += 1;
for (int i = 0; pos + i < len && i < base.length(); i++) {
data[pos + i] = base.charAt(i);
}

return new String(data);

}

/**
Expand All @@ -465,7 +458,7 @@ public static String lpad(String base, Integer len, String pad)
* @param base The base string to be padded
* @param len The length of padded string
* @param pad The pad string
* @return the string right-padded with pad to a lenght of len
* @return the string right-padded with pad to a length of len
*/
public static String rpad(String base, Integer len, String pad)
{
Expand All @@ -476,25 +469,19 @@ public static String rpad(String base, Integer len, String pad)
}

char[] data = new char[len];
char[] baseChars = base.toCharArray();
char[] padChars = pad.toCharArray();

int pos = 0;

// Copy the base
while (pos < base.length() && pos < len) {
data[pos] = baseChars[pos];
pos += 1;
for ( ; pos < base.length() && pos < len; pos++) {
data[pos] = base.charAt(pos);
}

// Copy the padding
while (pos < len) {
int i = 0;
while (i < pad.length() && i < len - pos) {
data[pos + i] = padChars[i];
i += 1;
for ( ; pos < len; pos += pad.length()) {
for (int i = 0; i < pad.length() && i < len - pos; i++) {
data[pos + i] = pad.charAt(i);
}
pos += pad.length();
}

return new String(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public void testLpad()
assertExpr("lpad(x, 4, 'ab')", "afoo");
assertExpr("lpad(x, 2, 'ab')", "fo");
assertExpr("lpad(x, 0, 'ab')", null);
assertExpr("lpad(x, 5, null)", null);
assertExpr("lpad(null, 5, x)", null);
}

@Test
Expand All @@ -151,5 +153,7 @@ public void testRpad()
assertExpr("rpad(x, 4, 'ab')", "fooa");
assertExpr("rpad(x, 2, 'ab')", "fo");
assertExpr("rpad(x, 0, 'ab')", null);
assertExpr("rpad(x, 5, null)", null);
assertExpr("rpad(null, 5, x)", null);
}
}

0 comments on commit 7d3474a

Please sign in to comment.