8000 Share code for line wrapping in a utility class · srijeet0406/dnsjava@0885fad · GitHub
[go: up one dir, main page]

Skip to content

Commit 0885fad

Browse files
committed
Share code for line wrapping in a utility class
1 parent e446215 commit 0885fad

File tree

4 files changed

+87
-15
lines changed

4 files changed

+87
-15
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
package org.xbill.DNS.utils;
3+
4+
import lombok.experimental.UtilityClass;
5+
6+
@UtilityClass
7+
class BaseUtils {
8+
/**
9+
* Wrap a long string at {@code lineLength} characters.
10+
*
11+
* @param s The string to wrap.
12+
* @param lineLength The number of characters per line.
13+
* @param prefix A string prefixing the characters on each line.
14+
* @param addClose Whether to add a close parenthesis or not.
15+
* @return The wrapped string.
16+
*/
17+
String wrapLines(String s, int lineLength, String prefix, boolean addClose) {
18+
StringBuilder sb = 8000 new StringBuilder();
19+
for (int i = 0; i < s.length(); i += lineLength) {
20+
sb.append(prefix);
21+
if (i + lineLength >= s.length()) {
22+
sb.append(s.substring(i));
23+
if (addClose) {
24+
sb.append(" )");
25+
}
26+
} else {
27+
sb.append(s, i, i + lineLength);
28+
sb.append("\n");
29+
}
30+
}
31+
return sb.toString();
32+
}
33+
}

src/main/java/org/xbill/DNS/utils/base16.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ public static String toString(byte[] b) {
3535
return sb.toString();
3636
}
3737

38+
/**
39+
* Convert binary data to a hex-encoded string, line-wrapped at {@code lineLength} characters.
40+
*
41+
* @param b An array containing binary data
42+
* @param lineLength The number of characters per line
43+
* @param prefix A string prefixing the characters on each line
44+
* @param addClose Whether to add a close parenthesis or not
45+
* @return A String containing the encoded data
46+
* @since 3.6
47+
*/
48+
public static String toString(byte[] b, int lineLength, String prefix, boolean addClose) {
49+
return BaseUtils.wrapLines(toString(b), lineLength, prefix, addClose);
50+
}
51+
3852
/**
3953
* Convert a hex-encoded String to binary data, ignoring {@link Character#isWhitespace(char)
4054
* whitespace} characters.

src/main/java/org/xbill/DNS/utils/base64.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line nu 8000 mberDiff line change
@@ -87,21 +87,7 @@ public static String toString(byte[] b, boolean useUrl) {
8787
* @return A String representing the formatted output
8888
*/
8989
public static String formatString(byte[] b, int lineLength, String prefix, boolean addClose) {
90-
String s = toString(b);
91-
StringBuilder sb = new StringBuilder();
92-
for (int i = 0; i < s.length(); i += lineLength) {
93-
sb.append(prefix);
94-
if (i + lineLength >= s.length()) {
95-
sb.append(s.substring(i));
96-
if (addClose) {
97-
sb.append(" )");
98-
}
99-
} else {
100-
sb.append(s, i, i + lineLength);
101-
sb.append("\n");
102-
}
103-
}
104-
return sb.toString();
90+
return BaseUtils.wrapLines(toString(b), lineLength, prefix, addClose);
10591
}
10692

10793
/**
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
package org.xbill.DNS.utils;
3+
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.ValueSource;
9+
10+
public class BaseUtilsTest {
11+
@ParameterizedTest
12+
@ValueSource(ints = {8, 10, 24, 63, 64, 65})
13+
void testWrapLines(int lineLength) {
14+
String toWrap = String.format("%0" + ((lineLength * 3) + 5) + "d", 0);
15+
String out = BaseUtils.wrapLines(toWrap, lineLength, "", false);
16+
St 57A0 ring[] lines = out.split("\n");
17+
assertEquals(4, lines.length);
18+
assertEquals(5, lines[3].length());
19+
}
20+
21+
@ParameterizedTest
22+
@ValueSource(ints = {8, 10, 24, 63, 64, 65})
23+
void testWrapLinesEndsWith(int lineLength) {
24+
String toWrap = String.format("%0" + ((lineLength * 3) + 5) + "d", 0);
25+
String out = BaseUtils.wrapLines(toWrap, lineLength, "", true);
26+
assertEquals(')', out.charAt(out.length() - 1));
27+
}
28+
29+
@ParameterizedTest
30+
@ValueSource(ints = {8, 10, 24, 63, 64, 65})
31+
void testWrapLinesPrefix(int lineLength) {
32+
String toWrap = String.format("%0" + ((lineLength * 3) + 5) + "d", 0);
33+
String out = BaseUtils.wrapLines(toWrap, lineLength, "\t", false);
34+
String[] lines = out.split("\n");
35+
for (String line : lines) {
36+
assertTrue(line.startsWith("\t"), "Line start with prefix");
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)
0