code
stringlengths
25
201k
docstring
stringlengths
19
96.2k
func_name
stringlengths
0
235
language
stringclasses
1 value
repo
stringlengths
8
51
path
stringlengths
11
314
url
stringlengths
62
377
license
stringclasses
7 values
public static String centerString(String s, int width, char c) { StringBuilder paddedString = new StringBuilder (width); int paddingNeeded; int len = s.length(); int frontPadding; int tailPadding; int i; paddingNeeded = (width < len) ? 0 : (width - len); i = paddingNeeded / 2; frontPadding = i; tailPadding = i + (paddingNeeded % 2); for (i = 0; i < frontPadding; i++) paddedString.append(c); paddedString.append(s); for (i = 0; i < tailPadding; i++) paddedString.append(c); return paddedString.toString(); }
Center a string in a fixed-width field, using the specified character for padding. If the string is already longer than the field width, it is returned unchanged. @param s the string @param width the desired field width @param c the pad character @return a right-justified version of the string @see #centerString(String,int,char) @see #leftJustifyString(String,int,char) @see #rightJustifyString(String,int,char)
centerString
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static StringBuilder hexadecimalForByte(byte b, StringBuilder buf) { int nybble; // High nybble first nybble = (b >>> 4) & 0x0f; buf.append(HEXADECIMAL_DIGITS[nybble]); // Now the low nybble nybble = b & 0x0f; buf.append(HEXADECIMAL_DIGITS[nybble]); return buf; }
Encode a byte as a two-digit hexadecimal string, appending the result to a <tt>StringBuilder</tt>. The hexadecimal string uses lower-case digits. Note that the <tt>byte</tt> type in Java is signed. Use the other version of {@link #hexadecimalForByte(int,StringBuilder)} if you want to pass an unsigned byte value. @param b the byte @param buf the <tt>StringBuilder</tt> to which to append the hex string @return a reference to the <tt>StringBuilder</tt>, for convenience @see #hexadecimalForByte(int,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #hexadecimalForBytes(int[],int,int,StringBuilder) @see #bytesForHexadecimal(String)
hexadecimalForByte
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static StringBuilder hexadecimalForByte (int b, StringBuilder buf) { int nybble; // High nybble first nybble = (b >>> 4) & 0x0f; buf.append(HEXADECIMAL_DIGITS[nybble]); // Now the low nybble nybble = b & 0x0f; buf.append(HEXADECIMAL_DIGITS[nybble]); return buf; }
Encode a byte as a two-digit hexadecimal string, appending the result to a <tt>StringBuilder</tt>.The hexadecimal string uses lower-case digits. in lower-case. This version of <tt>hexadecimalForByte</tt> is suitable for use with unsigned values. @param b the byte @param buf the <tt>StringBuilder</tt> to which to append the hex string @return a reference to the <tt>StringBuilder</tt>, for convenience @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #hexadecimalForBytes(int[],int,int,StringBuilder) @see #bytesForHexadecimal(String)
hexadecimalForByte
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static StringBuilder hexadecimalForBytes(byte[] bytes, int start, int end, StringBuilder buf) { for (int i = start; i < end; i++) hexadecimalForByte(bytes[i], buf); return buf; }
Encode an array of bytes as a hexadecimal string, appending the result to a <tt>StringBuilder</tt>. Note that the <tt>byte</tt> type in Java is signed. Use the other version of {@link #hexadecimalForBytes(int[],int,int,StringBuilder)} if you want to pass signed byte values. @param bytes The array of bytes @param start starting index in byte array @param end one past ending index in byte array (thus, bytes.length is a valid value) @param buf Where to append the result @return a reference to the <tt>StringBuilder</tt>, for convenience @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForByte(int,StringBuilder) @see #bytesForHexadecimal(String)
hexadecimalForBytes
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static String hexadecimalForBytes(byte[] bytes) { return hexadecimalForBytes(bytes, 0, bytes.length, new StringBuilder()) .toString(); }
Encode an array of bytes as a hexadecimal string, returning the string. Calling this method is equivalent to: <blockquote> <pre> hexadecimalForBytes(buf, 0, buf.length, new StringBuilder()).toString() </pre> </blockquote> @param bytes The array of bytes @return a reference to the <tt>StringBuilder</tt>, for convenience @see #hexadecimalForBytes(byte[],int,int,StringBuilder)
hexadecimalForBytes
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static StringBuilder hexadecimalForBytes(int[] bytes, int start, int end, StringBuilder buf) { for (int i = start; i < end; i++) hexadecimalForByte(bytes[i], buf); return buf; }
Encode an array of bytes as a hexadecimal string, appending the result to a <tt>StringBuilder</tt>. Note that this method takes an <tt>int</tt> array; however, it only examines the low-order byte of each integer. It's suitable for use with unsigned values. Use the other version of {@link #hexadecimalForBytes(byte[],int,int,StringBuilder)} if you want to pass signed byte values. @param bytes The array of bytes @param start starting index in byte array @param end one past ending index in byte array (thus, bytes.length is a valid value) @param buf Where to append the result @return the <tt>StringBuilder</tt>, for convenience @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForByte(int,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #bytesForHexadecimal(String)
hexadecimalForBytes
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static StringBuilder hexadecimalForNumber(int num, StringBuilder buf) { return encodeNumber(num, HEXADECIMAL_DIGITS.length, HEXADECIMAL_DIGITS, buf); }
Encode an integer as a hexadecimal string, appending the result to a <tt>StringBuilder</tt>. This method uses <tt>encodeNumber()</tt> to perform the actual encoding. @param num The integer to encode @param buf Where to append the result @return the <tt>StringBuilder</tt>, for convenience @see #encodeNumber(int,int,char[],StringBuilder) @see #hexadecimalForNumber(short,StringBuilder) @see #hexadecimalForNumber(long,StringBuilder) @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForByte(int,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #hexadecimalForBytes(int[],int,int,StringBuilder)
hexadecimalForNumber
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static String hexadecimalForNumber(int num) { return encodeNumber(num, HEXADECIMAL_DIGITS.length, HEXADECIMAL_DIGITS, new StringBuilder()).toString(); }
Encode an integer as a hexadecimal string, returning the hex string. This method uses <tt>encodeNumber()</tt> to perform the actual encoding. @param num The integer to encode @return the <tt>StringBuilder</tt>, for convenience @see #encodeNumber(int,int,char[],StringBuilder) @see #hexadecimalForNumber(short) @see #hexadecimalForNumber(int,StringBuilder) @see #hexadecimalForNumber(long) @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForByte(int,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #hexadecimalForBytes(int[],int,int,StringBuilder)
hexadecimalForNumber
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static StringBuilder hexadecimalForNumber(long num, StringBuilder buf) { return encodeNumber(num, HEXADECIMAL_DIGITS.length, HEXADECIMAL_DIGITS, buf); }
Encode a long as a hexadecimal string, appending the result to a <tt>StringBuilder</tt>. @param num The long to encode @param buf Where to append the result @return the <tt>StringBuilder</tt>, for convenience @see #encodeNumber(long,int,char[],StringBuilder) @see #hexadecimalForNumber(int,StringBuilder) @see #hexadecimalForNumber(short,StringBuilder) @see #hexadecimalForNumber(long) @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForByte(int,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #hexadecimalForBytes(int[],int,int,StringBuilder)
hexadecimalForNumber
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static String hexadecimalForNumber(long num) { return encodeNumber(num, HEXADECIMAL_DIGITS.length, HEXADECIMAL_DIGITS, new StringBuilder()).toString(); }
Encode a long as a hexadecimal string, returning the hex string. This method uses <tt>encodeNumber()</tt> to perform the actual encoding. @param num The long integer to encode @return the <tt>StringBuilder</tt>, for convenience @see #encodeNumber(int,int,char[],StringBuilder) @see #hexadecimalForNumber(short) @see #hexadecimalForNumber(long,StringBuilder) @see #hexadecimalForNumber(int) @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForByte(int,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #hexadecimalForBytes(int[],int,int,StringBuilder)
hexadecimalForNumber
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static String hexadecimalForNumber(short num) { return encodeNumber(num, HEXADECIMAL_DIGITS.length, HEXADECIMAL_DIGITS, new StringBuilder()).toString(); }
Encode a short as a hexadecimal string, returning the hex string. This method uses <tt>encodeNumber()</tt> to perform the actual encoding. @param num The short integer to encode @return the <tt>StringBuilder</tt>, for convenience @see #encodeNumber(int,int,char[],StringBuilder) @see #hexadecimalForNumber(long) @see #hexadecimalForNumber(short,StringBuilder) @see #hexadecimalForNumber(int) @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForByte(int,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #hexadecimalForBytes(int[],int,int,StringBuilder)
hexadecimalForNumber
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static StringBuilder hexadecimalForNumber(short num, StringBuilder buf) { return encodeNumber(num, HEXADECIMAL_DIGITS.length, HEXADECIMAL_DIGITS, buf); }
Encode a short as a hexadecimal string, appending the result to a <tt>StringBuilder</tt>. @param num The short integer to encode @param buf Where to append the result @return the <tt>StringBuilder</tt>, for convenience @see #hexadecimalForNumber(int,StringBuilder) @see #hexadecimalForNumber(long,StringBuilder) @see #hexadecimalForNumber(short) @see #encodeNumber(short,int,char[],StringBuilder) @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForByte(int,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #hexadecimalForBytes(int[],int,int,StringBuilder)
hexadecimalForNumber
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static StringBuilder encodeNumber(long num, int base, char[] digits, StringBuilder buf) { // Encode backwards; it's easier, though undoubtedly much slower. // Worst case scenario: Base is 2, and we need 64 digits. int digit; char[] temp = new char[64]; int i = 0; while (num != 0) { digit = (int) (num % base); num = num / base; temp[i++] = digits[digit]; } // Special case: num was 0 to begin with if (i == 0) temp[i++] = digits[0]; while (--i >= 0) buf.append(temp[i]); return buf; }
<p>Encode a long integer in an arbitrary base; the caller specifies an array of digit characters to be used for the encoding. For instance, to encode a number in base 36, you might use the following code fragment:</p> <blockquote> <pre> {@code StringBuilder buf = new StringBuilder(); long num = ... char[] digits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; TextUtils.encodeNumber (num, digits.length, digits, buf); } </pre> </blockquote> <p>For convenience, this class provides a character array of hexadecimal digits. (See {@link #HEXADECIMAL_DIGITS}.) However, it's usually simpler just to use one of the <tt>hexadecimalForNumber()</tt> methods().</p> @param num The long integer to encode @param base The base in which to encode the number @param digits The array of digit characters to use for the encoding. This array must be at least <tt>base</tt> characters in length. <tt>digits[0]</tt> represents the numeral to use for 0, <tt>digits[1]</tt> represents the numeral to use for 1, etc. @param buf The <tt>StringBuilder</tt> to which to append the encoded number string @return the <tt>StringBuilder</tt>, for convenience @see #encodeNumber(short,int,char[],StringBuilder) @see #encodeNumber(int,int,char[],StringBuilder) @see #hexadecimalForNumber(int,StringBuilder) @see #hexadecimalForNumber(short,StringBuilder) @see #hexadecimalForNumber(long,StringBuilder) @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForByte(int,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #hexadecimalForBytes(int[],int,int,StringBuilder)
encodeNumber
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static StringBuilder encodeNumber(short num, int base, char[] digits, StringBuilder buf) { // Encode backwards; it's easier, though undoubtedly much slower. // Worst case scenario: Base is 2, and we need 16 digits. int digit; char[] temp = new char[16]; int i = 0; while (num != 0) { digit = (num % base); num = (short) (num / base); temp[i++] = digits[digit]; } // Special case: num was 0 to begin with if (i == 0) temp[i++] = digits[0]; while (--i >= 0) buf.append(temp[i]); return buf; }
<p>Encode a short integer in an arbitrary base; the caller specifies an array of digit characters to be used for the encoding. For instance, to encode a number in base 36, you might use the following code fragment:</p> <blockquote> <pre> {@code StringBuilder buf = new StringBuilder(); short num = ... char[] digits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; TextUtils.encodeNumber (num, digits.length, digits, buf); } </pre> </blockquote> <p>For convenience, this class provides a character array of hexadecimal digits. (See {@link #HEXADECIMAL_DIGITS}.) However, it's usually simpler just to use one of the <tt>hexadecimalForNumber()</tt> methods().</p> @param num The short integer to encode @param base The base in which to encode the number @param digits The array of digit characters to use for the encoding. This array must be at least <tt>base</tt> characters in length. <tt>digits[0]</tt> represents the numeral to use for 0, <tt>digits[1]</tt> represents the numeral to use for 1, etc. @param buf The <tt>StringBuilder</tt> to which to append the encoded number string @return the <tt>StringBuilder</tt>, for convenience @see #encodeNumber(int,int,char[],StringBuilder) @see #encodeNumber(long,int,char[],StringBuilder) @see #hexadecimalForNumber(int,StringBuilder) @see #hexadecimalForNumber(short,StringBuilder) @see #hexadecimalForNumber(long,StringBuilder) @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForByte(int,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #hexadecimalForBytes(int[],int,int,StringBuilder)
encodeNumber
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static StringBuilder encodeNumber (int num, int base, char[] digits, StringBuilder buf) { // Encode backwards; it's easier, though undoubtedly much slower. // Worst case scenario: Base is 2, and we need 32 digits. int digit; char[] temp = new char[32]; int i = 0; while (num != 0) { digit = (num % base); num = num / base; temp[i++] = digits[digit]; } // Special case: num was 0 to begin with if (i == 0) temp[i++] = digits[0]; while (--i >= 0) buf.append(temp[i]); return buf; }
<p>Encode an integer in an arbitrary base; the caller specifies an array of digit characters to be used for the encoding. For instance, to encode a number in base 36, you might use the following code fragment:</p> <blockquote> <pre> {@code StringBuilder buf = new StringBuilder(); int num = ... char[] digits = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; TextUtils.encodeNumber (num, digits.length, digits, buf); } </pre> </blockquote> <p>For convenience, this class provides a character array of hexadecimal digits. (See {@link #HEXADECIMAL_DIGITS}.) However, it's usually simpler just to use one of the <tt>hexadecimalForNumber()</tt> methods().</p> @param num The integer to encode @param base The base in which to encode the number @param digits The array of digit characters to use for the encoding. This array must be at least <tt>base</tt> characters in length. <tt>digits[0]</tt> represents the numeral to use for 0, <tt>digits[1]</tt> represents the numeral to use for 1, etc. @param buf The <tt>StringBuilder</tt> to which to append the encoded number string @return the <tt>StringBuilder</tt>, for convenience @see #encodeNumber(short,int,char[],StringBuilder) @see #encodeNumber(long,int,char[],StringBuilder) @see #hexadecimalForNumber(int,StringBuilder) @see #hexadecimalForNumber(short,StringBuilder) @see #hexadecimalForNumber(long,StringBuilder) @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForByte(int,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #hexadecimalForBytes(int[],int,int,StringBuilder)
encodeNumber
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static byte[] bytesForHexadecimal(String hexString) throws NumberFormatException { byte[] result = null; // Break the string into white-space delimited tokens, and process // the tokens. Each token must have an even number of characters. StringBuilder stripped = new StringBuilder(); StringTokenizer tok = new StringTokenizer (hexString); while (tok.hasMoreTokens()) stripped.append(tok.nextToken()); // Now, stripped contains the original string without white space. // Make sure it's the right length. int len = stripped.length(); if ((len % 2) != 0) { throw new NumberFormatException("Hex string \"" + hexString + "\" does not have an even number " + "of hex digits."); } hexString = stripped.toString(); // Allocate the resulting byte array. result = new byte[len / 2]; // Now, parse the stripped string. int i; int j; for (i = 0, j = 0; i < len; i+= 2, j++) { String hexByte = hexString.substring(i, i + 2); // Note: We use Integer.parseInt(String,radix) because // Byte.parseByte(String,radix) won't properly parse anything // larger than "7f". Bytes hold values in the range [-128, // 127]. A value larger than "7f" ought to result in the sign // bit being set, but the Byte class uses the Integer class to // parse the value, and then range-checks the resulting // integer. In an integer, "80" doesn't set the sign bit // (though it would, in a byte), so it fails. There appears to // be no way to encode a negative byte value in hex so that it // can be read back in later, at least not using the standard // JDK. So we do it ourselves. result[j] = (byte) (Integer.parseInt(hexByte, 16) & 0x00ff); } return result; }
<p>Takes a string of hexadecimal-encoded byte values and converts it back to an array of bytes. The string may or may not contain white space; all white space is stripped before parsing begins. However, white space cannot occur between the digits for a byte; it must only occur between bytes. That is, "6f a1" is legal, while "6 f a 1" is not. Each byte must be encoded as a 2-digits hexadecimal value; as a consequence, the input string must have an even number of characters, once all white space has been stripped. The hexadecimal digits "a" through "f" may be in upper- or lower-case. An illegal hexadecimal string will result in a <tt>NumberFormatException</tt>. The following strings are examples of legal input to this method:</p> <blockquote> <pre> 1F 66756c6c74696c742e636f6d 42 72 69 61 6e 20 43 6c 61 70 70 65 72 49 2D41 63 63656C </pre> </blockquote> <p>The following strings are examples of illegal input:</p> <blockquote> <pre> 1X 6 6f a 1 6675-6c6c-7469-6c742e-636f6d </pre> </blockquote> @param hexString the hex string to convert @return an array of bytes representing the decoded hex string @throws NumberFormatException if the hex string is invalid @see #hexadecimalForByte(byte,StringBuilder) @see #hexadecimalForByte(int,StringBuilder) @see #hexadecimalForBytes(byte[],int,int,StringBuilder) @see #hexadecimalForBytes(int[],int,int,StringBuilder)
bytesForHexadecimal
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static String romanNumeralsForNumber(int n) { assert (n > 0); StringBuilder buf = new StringBuilder(); for (RomanNumberTableEntry tableEntry : ROMAN_NUMBER_TABLE) { while (n >= tableEntry.quantity) { buf.append(tableEntry.romanString); n -= tableEntry.quantity; } } return buf.toString(); }
Get the (upper-case) Roman numeral string for a number. @param n the number; must be positive @return the roman number string
romanNumeralsForNumber
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static String charToUnicodeEscape(char c, StringBuilder buf) { String hex = Integer.toHexString((int) c); buf.append("\\u"); for (int i = hex.length(); i < 4; i++) buf.append('0'); buf.append(hex); return buf.toString(); }
Convert a character to its corresponding Unicode escape sequence. @param c the character @param buf where to store the result @return the contents of <tt>buf</tt>, as a string @see #charToUnicodeEscape(char)
charToUnicodeEscape
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static String charToUnicodeEscape(char c) { return charToUnicodeEscape(c, new StringBuilder()); }
Convert a character to its corresponding Unicode escape sequence. @param c the character @return the Unicode escape sequence for the character @see #charToUnicodeEscape(char)
charToUnicodeEscape
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public static boolean isPrintable(char c) { boolean isPrintable = false; Character.UnicodeBlock ublock; StringBuilder result = new StringBuilder(); ublock = Character.UnicodeBlock.of (c); if ((ublock == Character.UnicodeBlock.BASIC_LATIN) || (ublock == Character.UnicodeBlock.LATIN_1_SUPPLEMENT) || (ublock == Character.UnicodeBlock.LATIN_EXTENDED_A)) { // Might be printable. int type = Character.getType (c); switch (type) { case Character.OTHER_PUNCTUATION: case Character.START_PUNCTUATION: case Character.END_PUNCTUATION: case Character.CONNECTOR_PUNCTUATION: case Character.CURRENCY_SYMBOL: case Character.MATH_SYMBOL: case Character.MODIFIER_SYMBOL: case Character.UPPERCASE_LETTER: case Character.LOWERCASE_LETTER: case Character.DECIMAL_DIGIT_NUMBER: case Character.DASH_PUNCTUATION: case Character.SPACE_SEPARATOR: isPrintable = true; break; } } return isPrintable; }
<p>Determine whether a character is printable. This method uses a simple definition of "printable" that doesn't take into account specific locales. A character is assumed to be printable if (a) it's in the Basic Latin, Latin 1 Supplement, or Extended Latin A Unicode block, and (b) its type, as returned by <tt>java.lang.Character.getType()</tt> is one of:</p> <ul> <li><tt>Character.OTHER_PUNCTUATION</tt> <li><tt>Character.START_PUNCTUATION</tt> <li><tt>Character.END_PUNCTUATION</tt> <li><tt>Character.CONNECTOR_PUNCTUATION</tt> <li><tt>Character.CURRENCY_SYMBOL</tt> <li><tt>Character.MATH_SYMBOL</tt> <li><tt>Character.MODIFIER_SYMBOL</tt> <li><tt>Character.UPPERCASE_LETTER</tt> <li><tt>Character.LOWERCASE_LETTER</tt> <li><tt>Character.DECIMAL_DIGIT_NUMBER</tt> <li><tt>Character.SPACE_SEPARATOR</tt> <li><tt>Character.DASH_PUNCTUATION</tt> </ul> <p>All other characters are assumed to be non-printable, even if they could actually be printed in the current locale or on some printer. @param c the character to test @return whether or not it is non-
isPrintable
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/TextUtil.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/TextUtil.java
MIT
public boolean getHonorEscapes() { return honorEscapes; }
Get the value of the flag that controls whether or not the <tt>substitute()</tt> method will honor backslash escapes within variable references. @return <tt>true</tt> if backslash escapes are honored, <tt>false</tt> if not. @see #setHonorEscapes
getHonorEscapes
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/UnixShellVariableSubstituter.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/UnixShellVariableSubstituter.java
MIT
public void setHonorEscapes(boolean enable) { this.honorEscapes = enable; }
Set the value of the flag that controls whether or not the <tt>substitute()</tt> method will honor backslash escapes within variable references. The flag defaults to <tt>false</tt>. @param enable <tt>true</tt> if backslash escapes should be honored, <tt>false</tt> if not. @see #getHonorEscapes
setHonorEscapes
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/UnixShellVariableSubstituter.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/UnixShellVariableSubstituter.java
MIT
public String substitute(String s, VariableDereferencer deref, VariableNameChecker nameChecker, Object context, boolean allowEscapes) throws VariableSyntaxException, UndefinedVariableException, VariableSubstitutionException { boolean save = getHonorEscapes(); setHonorEscapes(allowEscapes); try { return substitute(s, deref, nameChecker, context); } finally { setHonorEscapes(save); } }
<p>Substitute all variable references in the supplied string, using a Unix Bourne Shell-style variable syntax. This method uses a supplied <tt>VariableDereferencer</tt> object to resolve variable values. Note that this method throws no exceptions. Syntax errors in the variable references are silently ignored. Variables that have no value are substituted as the empty string. If the <tt>nameChecker</tt> parameter is not null, this method calls its {@link VariableNameChecker#legalVariableCharacter(char)} method to determine whether a given character is a legal part of a variable name. If <tt>nameChecker</tt> is null, then this method assumes that variable names may consist solely of alphanumeric characters, underscores and periods. This syntax is sufficient to substitute variables from <tt>System.properties</tt>, for instance.</p> @param s the string containing possible variable references @param deref the <tt>VariableDereferencer</tt> object to use to resolve the variables' values. @param nameChecker the <tt>VariableNameChecker</tt> object to be used to check for legal variable name characters, or null @param context an optional context object, passed through unmodified to the <tt>deref</tt> object's {@link VariableDereferencer#getVariableValue} method. This object can be anything at all (and, in fact, may be null if you don't care.) It's primarily useful for passing context information from the caller to the <tt>VariableDereferencer</tt>. @param allowEscapes whehther or not escape sequences are honored @return The (possibly) expanded string. @throws UndefinedVariableException undefined variable, and {@link #getAbortOnUndefinedVariable} returns true @throws VariableSyntaxException syntax error, and {@link #getAbortOnSyntaxError} returns true @throws VariableSubstitutionException substitution error @see #substitute(String,VariableDereferencer,Object) @see VariableDereferencer#getVariableValue(String,Object) @deprecated As of version 2.3; please use {@link #setHonorEscapes}
substitute
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/UnixShellVariableSubstituter.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/UnixShellVariableSubstituter.java
MIT
public String substitute(String s, VariableDereferencer deref, VariableNameChecker nameChecker, Object context) throws VariableSyntaxException, UndefinedVariableException, VariableSubstitutionException { StringBuilder result = new StringBuilder(); int len = s.length(); StringBuilder var = new StringBuilder(); StringBuilder defaultValue = new StringBuilder(); ParseState state = ParseState.NOT_IN_VAR; boolean braces = false; boolean nextIsLiteral = false; boolean syntaxError = false; int i; char ch[]; if (nameChecker == null) nameChecker = this; ch = s.toCharArray(); i = 0; while ((i < len) && (! syntaxError)) { char c = ch[i++]; if (nextIsLiteral) { // Literal result.append(c); nextIsLiteral = false; } else if (state == ParseState.NOT_IN_VAR) { // Not in a variable. if (c == VAR_START) // Possible start of new variable. state = ParseState.IN_VAR; else if (honorEscapes && (c == '\\')) // next char is literal nextIsLiteral = true; else // Just a regular old character. result.append(c); } else if (state == ParseState.IN_DEFAULT_VALUE) { if (c == VAR_CLOSE_BRACE) { state = ParseState.IN_VAR; i--; } else { defaultValue.append(c); } } // If we get here, we're currently assembling a variable name. else if ( (var.length() == 0) && (c == VAR_OPEN_BRACE) ) { // start of ${...} sequence braces = true; } else if (braces && (c == VAR_IF_EXISTS_OP)) { // Specification of default value. state = ParseState.IN_DEFAULT_VALUE; } else if (nameChecker.legalVariableCharacter(c)) { var.append (c); } else { // Not a legal variable character, so we're done assembling // this variable name. String varName = var.toString(); state = ParseState.NOT_IN_VAR; if (braces) { if (c == VAR_CLOSE_BRACE) // final brace; substitute { result.append(dereference(varName, defaultValue.toString(), context, deref)); } else // Missing trailing '}'. No substitution. { result.append(VAR_START); result.append(VAR_OPEN_BRACE); result.append(var.toString()); i--; // push 'c' back on the stack syntaxError = true; } braces = false; } else if (var.length() == 0) { // '$' followed by something illegal. Syntax error. result.append (VAR_START); i--; // push 'c' back on the stack syntaxError = true; } else { // Legal, non-bracketed variable. Substitute. result.append(dereference(varName, defaultValue.toString(), context, deref)); i--; // push 'c' back on the stack } var.setLength (0); } } // end while if (state == ParseState.IN_VAR) { // One last variable to handle. if (braces) // No trailing '}'. Syntax error. { result.append(VAR_START); result.append(VAR_OPEN_BRACE); result.append(var.toString()); syntaxError = true; } else if (var.length() == 0) // just a trailing "$" { result.append(VAR_START); } else { result.append(dereference(var.toString(), defaultValue.toString(), context, deref)); } } if (syntaxError && (getAbortOnSyntaxError() == true)) { throw new VariableSyntaxException ("Syntax error in reference to variable \"" + var.toString() + "\""); } return result.toString(); }
<p>Substitute all variable references in the supplied string, using a Unix Bourne Shell-style variable syntax. This method uses a supplied <tt>VariableDereferencer</tt> object to resolve variable values. Note that this method throws no exceptions. Syntax errors in the variable references are silently ignored. Variables that have no value are substituted as the empty string. If the <tt>nameChecker</tt> parameter is not null, this method calls its {@link VariableNameChecker#legalVariableCharacter(char)} method to determine whether a given character is a legal part of a variable name. If <tt>nameChecker</tt> is null, then this method assumes that variable names may consist solely of alphanumeric characters, underscores and periods. This syntax is sufficient to substitute variables from <tt>System.properties</tt>, for instance.</p> @param s the string containing possible variable references @param deref the <tt>VariableDereferencer</tt> object to use to resolve the variables' values. @param nameChecker the <tt>VariableNameChecker</tt> object to be used to check for legal variable name characters, or null @param context an optional context object, passed through unmodified to the <tt>deref</tt> object's {@link VariableDereferencer#getVariableValue} method. This object can be anything at all (and, in fact, may be null if you don't care.) It's primarily useful for passing context information from the caller to the <tt>VariableDereferencer</tt>. @return The (possibly) expanded string. @throws UndefinedVariableException undefined variable, and {@link #getAbortOnUndefinedVariable} returns true @throws VariableSyntaxException syntax error, and {@link #getAbortOnSyntaxError} returns true @throws VariableSubstitutionException substitution error @see #substitute(String,VariableDereferencer,Object) @see VariableDereferencer#getVariableValue(String,Object)
substitute
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/UnixShellVariableSubstituter.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/UnixShellVariableSubstituter.java
MIT
public String substitute( String s, final VariableDereferencer deref, final VariableNameChecker nameChecker, final Object context) throws VariableSyntaxException, UndefinedVariableException, VariableSubstitutionException { if (s != null) s = doSubstitution(s, context, nameChecker, deref); return s; }
<p>Substitute all variable references in the supplied string, using a Unix Bourne Shell-style variable syntax. This method uses a supplied <tt>VariableDereferencer</tt> object to resolve variable values. Note that this method throws no exceptions. Syntax errors in the variable references are silently ignored. Variables that have no value are substituted as the empty string. If the <tt>nameChecker</tt> parameter is not null, this method calls its {@link VariableNameChecker#legalVariableCharacter(char)} method to determine whether a given character is a legal part of a variable name. If <tt>nameChecker</tt> is null, then this method assumes that variable names may consist solely of alphanumeric characters and underscores.</p> @param s the string containing possible variable references @param deref the <tt>VariableDereferencer</tt> object to use to resolve the variables' values. @param nameChecker the <tt>VariableNameChecker</tt> object to be used to check for legal variable name characters, or null @param context an optional context object, passed through unmodified to the <tt>deref</tt> object's {@link VariableDereferencer#getVariableValue} method. This object can be anything at all (and, in fact, may be null if you don't care.) It's primarily useful for passing context information from the caller to the <tt>VariableDereferencer</tt>. @return The (possibly) expanded string. @throws VariableSubstitutionException substitution error @see #substitute(String,VariableDereferencer,Object) @see VariableDereferencer#getVariableValue(String,Object)
substitute
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/WindowsCmdVariableSubstituter.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/WindowsCmdVariableSubstituter.java
MIT
private String doSubstitution(final String s, final Object context, VariableNameChecker nameChecker, final VariableDereferencer deref) throws VariableSubstitutionException { StringBuffer result = new StringBuffer(); int len = s.length(); char prev = '\0'; StringBuffer var = new StringBuffer(); boolean inVar = false; boolean syntaxError = false; char ch[]; if (nameChecker == null) nameChecker = this; ch = s.toCharArray(); for (int i = 0; i < len; i++) { char c = ch[i]; if (c == '%') { if (inVar) { if (prev == '%') { // Doubled "%". Insert one literal "%". inVar = false; result.append('%'); } else { // End of variable reference. If the variable name // is syntactically incorrect, just store the // entire original sequence in the result string. String varName = var.toString(); if (syntaxError) { result.append('%' + varName + '%'); } else { String value = deref.getVariableValue(varName, context); if (((value == null) || (value.length() == 0)) && (getAbortOnUndefinedVariable() == true)) { throw new UndefinedVariableException ("Variable \"" + varName + "\" is not defined."); } result.append(value == null ? "" : value); } var.setLength(0); inVar = false; syntaxError = false; prev = '\0'; // prevent match on trailing "%" } } else { // Possible start of a new variable. inVar = true; prev = c; } } else { // Not a '%' if (inVar) { var.append(c); if (! nameChecker.legalVariableCharacter(c)) syntaxError = true; } else { result.append(c); } prev = c; } } if (inVar) { // Never saw the trailing "%" for the last variable reference. // Transfer the characters buffered in 'var' into the result, // without modification. result.append('%'); result.append(var.toString()); syntaxError = true; } if (syntaxError && (getAbortOnSyntaxError() == true)) { throw new VariableSyntaxException ("Syntactically incorrect reference to variable \"" + var.toString() + "\""); } return result.toString(); }
Worker routine called by substitute() to perform the actual substitution on a non-null string. @param s the string containing possible variable references @param deref the <tt>VariableDereferencer</tt> object to use to resolve the variables' values. @param nameChecker the <tt>VariableNameChecker</tt> object to be used to check for legal variable name characters, or null @param context an optional context object, passed through unmodified to the <tt>deref</tt> object's {@link VariableDereferencer#getVariableValue} method. This object can be anything at all (and, in fact, may be null if you don't care.) It's primarily useful for passing context information from the caller to the <tt>VariableDereferencer</tt>. @return The (possibly) expanded string. @throws VariableSubstitutionException substitution error
doSubstitution
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/WindowsCmdVariableSubstituter.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/WindowsCmdVariableSubstituter.java
MIT
public XStringBufBase append (boolean val) { try { getBufferAsAppendable().append (String.valueOf (val)); } catch (IOException ex) { // Shouldn't happen here. } return this; }
Append the string representation of a <tt>boolean</tt> value to the buffer. @param val The boolean value. @return a reference to this object
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase append (char c) { try { getBufferAsAppendable().append (c); } catch (IOException ex) { } return this; }
Append the specified character to the buffer. If the buffer is already at its maximum length, the character is silently ignored. @param c The character to append. @return a reference to this object
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase append (char chars[]) { for (int i = 0; i < chars.length; i++) append (chars[i]); return this; }
Append the specified array of characters to the buffer. This method appends only as much of the string as will fit without causing the buffer to exceed its maximum length. @param chars The characters to append. @return a reference to this object
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase append (char chars[], int offset, int len) { Appendable buf = getBufferAsAppendable(); while (offset < len) { try { buf.append (chars[offset++]); } catch (IOException ex) { // Shouldn't happen here. } } return this; }
Append the specified characters in a character array to the buffer. This method appends only as much of the string as will fit without causing the buffer to exceed its maximum length. @param chars The characters to append. @param offset The index of the first character to append @param len The maximum number of characters to append @return a reference to this object
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase append (double val) { try { getBufferAsAppendable().append (String.valueOf (val)); } catch (IOException ex) { // Shouldn't happen here. } return this; }
Append the string representation of a <tt>double</tt> value to the buffer. @param val The double value. @return a reference to this object
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase append (float val) { try { getBufferAsAppendable().append (String.valueOf (val)); } catch (IOException ex) { // Shouldn't happen here. } return this; }
Append the string representation of a <tt>float</tt> value to the buffer. @param val The float value. @return a reference to this object
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase append (int val) { try { getBufferAsAppendable().append (String.valueOf (val)); } catch (IOException ex) { // Shouldn't happen here. } return this; }
Append the string representation of a <tt>int</tt> value to the buffer. @param val The int value. @return a reference to this object
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase append (long val) { try { getBufferAsAppendable().append (String.valueOf (val)); } catch (IOException ex) { // Shouldn't happen here. } return this; }
Append the string representation of a <tt>long</tt> value to the buffer. @param val The long value. @return a reference to this object
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase append (Object obj) { try { getBufferAsAppendable().append (obj.toString()); } catch (IOException ex) { // Shouldn't happen here. } return this; }
Append the string representation of an object to the buffer. @param obj The object whose string value is to be appended. @return a reference to this object
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase append (short val) { try { getBufferAsAppendable().append (String.valueOf (val)); } catch (IOException ex) { // Shouldn't happen here. } return this; }
Append the string representation of a <tt>short</tt> value to the buffer. @param val The short value. @return a reference to this object
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase append (String s) { try { getBufferAsAppendable().append (s); } catch (IOException ex) { // Shouldn't happen here. } return this; }
Append the specified string to the buffer. This method appends only as much of the string as will fit without causing the buffer to exceed its maximum length. @param s The string to append. @return a reference to this object
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase append (CharSequence csq) { try { getBufferAsAppendable().append (csq); } catch (IOException ex) { // Shouldn't happen here. } return this; }
Append the entire contents of the specified <tt>CharSequence</tt> to the buffer. This method appends only as much of the <tt>CharSequence</tt> as will fit without causing the buffer to exceed its maximum length. @param csq The <tt>CharSequence</tt> to append @return a reference to this object @see #append(CharSequence,int,int)
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase append (CharSequence csq, int start, int end) { try { getBufferAsAppendable().append (csq, start, end); } catch (IOException ex) { // Shouldn't happen here. } return this; }
Append a subsequence of the specified <tt>CharSequence</tt> to the buffer. This method appends only as much of the subsequence as will fit without causing the buffer to exceed its maximum length. @param csq The <tt>CharSequence</tt> to append @param start The starting index of the subsequence @param end One past the ending index of the subsequence @return a reference to this object @see #append(CharSequence)
append
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public char charAt (int index) throws IndexOutOfBoundsException { return getBufferAsCharSequence().charAt (index); }
Return the character at a specified index in the buffer. Indexes begin at 0. (i.e., The first character in the buffer has index 0.) @param index The index of the character to return @return The character at the specified index. @throws IndexOutOfBoundsException if <tt>index</tt> is negative or greater than or equal to <tt>length()</tt>
charAt
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public boolean delete (String substring) { int i; boolean deleted = false; i = indexOf (substring); if (i > -1) { deleteCharacters (i, i + substring.length()); deleted = true; } return deleted; }
Delete the first occurrence of a given substring in the buffer. with another substring. @param substring The substring to find and replace @return <tt>true</tt> if the replacement succeeded, <tt>false</tt> otherwise.
delete
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public void clear() { delete (0, length()); }
Removes all characters from the buffer leaving it empty.
clear
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase delete (int start, int end) throws IndexOutOfBoundsException { deleteCharacters (start, end); return this; }
Remove the characters in a substring of this <tt>XStringBuffer</tt>. The substring begins at the specified <tt>start</tt> and extends to the character at index <tt>end - 1</tt>, or to the end of the string, if no such character exists. If <tt>start</tt> is equal to <tt>end</tt>, no changes are made. @param start The beginning index, inclusive @param end The ending index, exclusive @return This object @throws IndexOutOfBoundsException if <tt>start</tt> is negative, greater than <tt>length()</tt>, or greater than <tt>end</tt>
delete
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public void encodeMetacharacters (int start, int end) throws IndexOutOfBoundsException, IOException { char chars[] = toString().toCharArray(); int i = 0; StringBuilder scratch = new StringBuilder(); clear(); try { while (i < start) append (chars[i++]); while (i < end) append (encodeOneMetacharacter (chars[i++], scratch)); while (i < chars.length) append (chars[i++]); } catch (ArrayIndexOutOfBoundsException ex) { throw new StringIndexOutOfBoundsException (String.valueOf (i)); } }
<p>Replaces certain characters in the string buffer with Java metacharacter ("backslash") sequences.</p> <ul> <li> A horizontal tab is replaced with <tt>\t</tt>. <li> A line feed is replaced with <tt>\n</tt>. <li> A carriage return is replaced with <tt>\r</tt>. <li> A form feed is replaced with <tt>\f</tt>. <li> A backslash is replaced by two backslashes. <li> Nonprintable characters are replaced with a <tt>&#92;u</tt><i>xxxx</i> sequence. </ul> <p>This method uses the same definition of "non-printable" as {@link TextUtil#isPrintable}.</p> @param start The beginning index, inclusive @param end The ending index, exclusive @throws StringIndexOutOfBoundsException if <tt>start</tt> is negative, or greater than <tt>length()</tt>, or greater than <tt>end</tt> @throws IOException I/O exception @see #encodeMetacharacters() @see #decodeMetacharacters(int, int)
encodeMetacharacters
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public void encodeMetacharacters() { try { encodeMetacharacters (0, this.length()); } catch (IndexOutOfBoundsException ex) { // Should never happen } catch (IOException ex) { // Should never happen } }
A version of {@link #encodeMetacharacters(int,int) encodeMetacharacters} that processes the entire string buffer. Calling this method is equivalent to: <blockquote> <pre> buf.encodeMetacharacters (0, buf.length()) </pre> </blockquote> @see #encodeMetacharacters(int, int) @see #decodeMetacharacters()
encodeMetacharacters
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public void decodeMetacharacters (int start, int end) throws StringIndexOutOfBoundsException { char chars[] = toString().toCharArray(); int i = 0; StringBuilder newBuf = new StringBuilder(); try { // Copy verbatim the region of characters prior to "start" while (i < start) newBuf.append (chars[i++]); // Process the region. First, allocate a PushbackReader than // can handle up to 5 characters (4 characters for a Unicode // code, plus the preceding "u"). String region = new String (chars, i, end - i); StringReader sr = new StringReader (region); PushbackReader pb = new PushbackReader (sr, 5); // Now, process the region. for (;;) { int c; if ((c = pb.read()) == -1) break; if (c == METACHAR_SEQUENCE_START) { if ((c = pb.read()) == -1) { // Incomplete metacharacter sequence at end of // region. Just pass along the backslash as is. newBuf.append ((char) c); } else { c = decodeMetacharacter (c, pb); if (c == -1) break; if (c == -2) // Bad unicode sequence newBuf.append (METACHAR_SEQUENCE_START); else newBuf.append ((char) c); } } else { newBuf.append ((char) c); } } // Copy verbatim the region of characters after "end" i = end; while (i < chars.length) newBuf.append (chars[i++]); } catch (ArrayIndexOutOfBoundsException ex) { throw new StringIndexOutOfBoundsException (String.valueOf (i)); } catch (IOException ex) { throw new StringIndexOutOfBoundsException(); } clear(); append (newBuf.toString()); }
Replaces any metacharacter sequences in a portion of the string buffer (such as those produced by {@link #encodeMetacharacters()} with their actual characters. @param start The beginning index, inclusive @param end The ending index, exclusive @throws StringIndexOutOfBoundsException if <tt>start</tt> is negative, or greater than <tt>length()</tt>, or greater than <tt>end</tt> @see #decodeMetacharacters() @see #encodeMetacharacters(int,int)
decodeMetacharacters
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public void decodeMetacharacters() { try { decodeMetacharacters (0, this.length()); } catch (StringIndexOutOfBoundsException ex) { // Should never happen } }
A version of {@link #decodeMetacharacters(int,int) decodeMetacharacters} that processes the entire string buffer. Calling this method is equivalent to: <blockquote> <pre> buf.decodeMetacharacters (0, buf.length()) </pre> </blockquote> @see #encodeMetacharacters(int, int) @see #decodeMetacharacters()
decodeMetacharacters
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public int indexOf (int ch) { return toString().indexOf (ch); }
Returns the index within this string of the first occurrence of the specified character. If a character with value <tt>ch</tt> occurs in the character sequence represented by this object, then the index (in Unicode code units) of the first such occurrence is returned. @param ch the cahracter (Unicode code point) @return the index of the first occurrence of the character, or -1 if not found
indexOf
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public int indexOf (String str) { return toString().indexOf (str); }
Returns the index within this string of the first occurrence of the specified substring in this sequence, starting at position 0. @param str the string to find @return the index of the first occurrence of the substring, or -1 if not found
indexOf
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public int indexOf (String str, int start) { return toString().indexOf (str, start); }
Returns the index within this string of the first occurrence of the specified substring in this sequence, with the search starting at the specified position. @param str the string to find @param start the index at which to start the search @return the index of the first occurrence of the substring, or -1 if not found
indexOf
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase insert (int index, boolean val) { return insert (index, String.valueOf (val)); }
Insert the string representation of a <tt>boolean</tt>value into the buffer at a specified position. Note that an insertion operation may push characters off the end of the buffer. @param index Where to start inserting in the string buffer @param val The <tt>boolean</tt> value @return this object
insert
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase insert (int index, char ch) { insertCharacter (index, ch); return this; }
Insert a single character at a specified position in the buffer. Note that an insertion operation may push * characters off the end of the buffer. @param index Where to start inserting in the string buffer @param ch The character to insert. @return this object
insert
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase insert (int index, char chars[]) { insertCharacters (index, chars, 0, chars.length); return this; }
Insert the contents of a character array into the buffer at a specified position. Note that an insertion operation may push characters off the end of the buffer. @param index Where to start inserting in the string buffer @param chars The character array. @return this object
insert
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase insert (int index, char chars[], int offset, int len) { insertCharacters (index, chars, offset, len); return this; }
Insert characters from a character array into the buffer at a specified position. Note that an insertion operation may push characters off the end of the buffer. @param index Where to start inserting in the string buffer @param chars The character array. @param offset The index of the first character to insert @param len The maximum number of characters to insert @return this object
insert
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase insert (int index, double val) { return insert (index, String.valueOf (val)); }
Insert the string representation of a <tt>double</tt>value into the buffer at a specified position. Note that an insertion operation may push characters off the end of the buffer. @param index Where to start inserting in the string buffer @param val The <tt>double</tt> value @return this object
insert
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase insert (int index, float val) { return insert (index, String.valueOf (val)); }
Insert the string representation of a <tt>float</tt>value into the buffer at a specified position. Note that an insertion operation may push characters off the end of the buffer. @param index Where to start inserting in the string buffer @param val The <tt>float</tt> value @return this object
insert
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase insert (int index, int val) { return insert (index, String.valueOf (val)); }
Insert the string representation of a <tt>int</tt>value into the buffer at a specified position. Note that an insertion operation may push characters off the end of the buffer. @param index Where to start inserting in the string buffer @param val The <tt>int</tt> value @return this object
insert
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase insert (int index, long val) { return insert (index, String.valueOf (val)); }
Insert the string representation of a <tt>long</tt>value into the buffer at a specified position. Note that an insertion operation may push characters off the end of the buffer. @param index Where to start inserting in the string buffer @param val The <tt>long</tt> value @return this object
insert
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase insert (int index, short val) { return insert (index, String.valueOf (val)); }
Insert the string representation of a <tt>short</tt>value into the buffer at a specified position. Note that an insertion operation may push characters off the end of the buffer. @param index Where to start inserting in the string buffer @param val The <tt>short</tt> value @return this object
insert
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase insert (int index, Object obj) { return insert (index, obj.toString()); }
Insert the string representation of an arbitrary object into the buffer at a specified position. Note that an insertion operation may push characters off the end of the buffer. @param index Where to start inserting in the string buffer @param obj The object whose string representation is to be inserted @return this object
insert
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase insert (int index, String s) { insertCharacters (index, s.toCharArray(), 0, s.length()); return this; }
Insert the contents of a string into the buffer at a specified position. Note that an insertion operation may push characters off the end of the buffer. @param index Where to start inserting in the string buffer @param s The string to insert @return this object
insert
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public int length() { return getBufferAsCharSequence().length(); }
Return the number of characters currently in the buffer. @return The number of characters in the buffer.
length
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public XStringBufBase replace (int start, int end, String str) throws StringIndexOutOfBoundsException { replaceString (start, end, str); return this; }
Replace the characters in a substring of this buffer with characters in the specified <tt>String</tt>. The substring begins at the specified <tt>start</tt> and extends to the character at index <tt>end - 1</tt>, or to the end of the <tt>XStringBuffer</tt> if no such character exists. First the characters in the substring are removed and then the specified <tt>String</tt> is inserted at <tt>start</tt>. (The <tt>XStringBuffer</tt> will be lengthened to accommodate the specified <tt>String</tt> if necessary.) @param start The beginning index, inclusive @param end The ending index, exclusive @param str The string that will replace the previous contents @return This object @throws StringIndexOutOfBoundsException if <tt>start</tt> is negative, or greater than <tt>length()</tt>, or greater than <tt>end</tt>
replace
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public boolean replace (String substring, String replacement) { int i; boolean replaced = false; i = indexOf (substring); if (i > -1) { replaceString (i, i + substring.length(), replacement); replaced = true; } return replaced; }
Replace the first occurrence of a given substring in the buffer with another substring. @param substring The substring to find and replace @param replacement The replacement string @return <tt>true</tt> if the replacement succeeded, <tt>false</tt> otherwise.
replace
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public boolean replace (String substring, char replacement) { int i; boolean replaced = false; i = indexOf (substring); if (i > -1) { deleteCharacters (i, i + substring.length()); insert (i, replacement); replaced = true; } return replaced; }
Replace the first occurrence of a given substring in the buffer with a given character @param substring The substring to find and replace @param replacement The replacement char @return <tt>true</tt> if the replacement succeeded, <tt>false</tt> otherwise.
replace
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public int replaceAll (String substring, String replacement) { StringBuilder buf = new StringBuilder (getBufferAsCharSequence()); int i; int start; int total = 0; start = 0; while ( (start < buf.length()) && ((i = buf.toString().indexOf (substring, start)) >= 0) ) { buf.replace (i, i + substring.length(), replacement); total++; start = i + replacement.length(); } this.clear(); this.append (buf.toString()); return total; }
Replace the all occurrences of a given substring in the buffer with another substring. This method avoids recursion; that is, it's safe even if the replacement string contains the source string. @param substring The substring to find and replace @param replacement The replacement string @return the number of replacements made
replaceAll
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public int replaceAll (char ch, char replacement) { int len = length(); char[] chars = new char[len]; int i; int total = 0; getChars (0, len, chars, 0); for (i = 0; i < len; i++) { if (chars[i] == ch) { chars[i] = replacement; total++; } } if (total > 0) { clear(); append (chars); } return total; }
Replace the all occurrences of a given character in the buffer with another character. @param ch The character to find and replace @param replacement The replacement character @return the number of replacements made
replaceAll
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public int replaceAll (char ch, String replacement) { return replaceAll ("" + ch, replacement); }
Replace the all occurrences of a given character in the buffer with string. This method avoids recursion; that is, it's safe even if the replacement string contains the character being replaced. @param ch The character to find and replace @param replacement The replacement string @return the number of replacements made
replaceAll
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public void reset (String str) { clear(); append (str); }
Removes all existing characters from the buffer and loads the string into the buffer. @param str <tt>String</tt> object to be loaded into the cleared buffer.
reset
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public String[] split() { return TextUtil.split (this.toString()); }
Split the contents of a buffer on white space, and return the resulting strings. This method is a convenient front-end to {@link TextUtil#split(String)}. @return an array of <tt>String</tt> objects @see #split(String) @see TextUtil#split(String,char)
split
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public String substring (int index) throws StringIndexOutOfBoundsException { return substring (index, length()); }
Return a new <tt>String</tt> that contains a subsequence of characters currently contained in this buffer. The substring begins at the specified index and extends to the end of the StringBuffer. @param index The beginning index, inclusive @return the substring @throws StringIndexOutOfBoundsException index out of range
substring
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public String substring (int start, int end) throws StringIndexOutOfBoundsException { return subSequence (start, end).toString(); }
Return a new <tt>String</tt> that contains a subsequence of characters currently contained in this buffer. The substring begins at the specified <tt>start</tt> and extends to the character at index <tt>end - 1</tt>. @param start The beginning index, inclusive @param end The beginning index, exclusive @return the substring @throws StringIndexOutOfBoundsException if <tt>start</tt> is negative, greater than <tt>length()</tt>, or greater than <tt>end</tt> @see #subSequence
substring
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public CharSequence subSequence (int start, int end) throws IndexOutOfBoundsException { return getBufferAsCharSequence().subSequence (start, end); }
Return a new <tt>CharSequence</tt> object (really, another <tt>XStringBuffer</tt>0 that contains a subsequence of characters currently contained in this buffer. The substring begins at the specified <tt>start</tt> and extends to the character at index <tt>end - 1</tt>. @param start The beginning index, inclusive @param end The beginning index, exclusive @return the subsequence @throws IndexOutOfBoundsException if <tt>start</tt> is negative, greater than <tt>length()</tt>, or greater than <tt>end</tt>
subSequence
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public String toString() { return getBufferAsCharSequence().toString(); }
Return the <tt>String</tt> representation of this buffer. @return The string.
toString
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
private static String encodeOneMetacharacter (char c, StringBuilder buf) { StringBuilder result = new StringBuilder(); if (TextUtil.isPrintable(c)) { if (c == METACHAR_SEQUENCE_START) { // Have to escape the escape character. result.append(METACHAR_SEQUENCE_START); result.append(METACHAR_SEQUENCE_START); } else { result.append(c); } } else { // Assume it's non-printable and translate it. switch (c) { case '\r': result.append("\\r"); break; case '\n': result.append("\\n"); break; case '\t': result.append("\\t"); break; case '\f': result.append("\\f"); break; default: result.append(toUnicodeEscape(c, buf)); } } return result.toString(); }
Escape a specific character, if it's non-printable. See the documentation for encodeMetacharacters() for the definition of non-printable. @param c the character to escape @param buf a scratch buffer, to avoid allocating a new one on each call @return the escape string (which might contain only the character passed in, if the character is printable)
encodeOneMetacharacter
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
private static String toUnicodeEscape (char c, StringBuilder buf) { buf.setLength (0); return TextUtil.charToUnicodeEscape(c, buf); }
Convert a character to a Unicode escape string. @param c the character to escape @param buf a scratch buffer, to avoid allocating a new one on each call @return the Unicode escape string
toUnicodeEscape
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
private int decodeMetacharacter (int c, PushbackReader pb) throws IOException { switch (c) { case 't': c = '\t'; break; case 'n': c = '\n'; break; case 'r': c = '\r'; break; case METACHAR_SEQUENCE_START: c = METACHAR_SEQUENCE_START; break; case 'u': c = decodeUnicodeSequence (pb); if (c == -2) { pb.unread ('u'); c = METACHAR_SEQUENCE_START; } break; default: // An escaped "regular" character is just the character. break; } return c; }
Decode a metacharacter sequence. @param c the character after the backslash @param pb a PushbackReader representing the remainder of the region being processed (necessary for Unicode sequences) @return the decoded metacharacter, -1 on EOF, -2 for unknown or bad sequence @throws IOException read error
decodeMetacharacter
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
private int decodeUnicodeSequence (PushbackReader pb) throws IOException { int c = -1; boolean incomplete = false; StringBuilder buf = new StringBuilder(); // Read four characters, each of which represents a single hex // digit. for (int i = 0; i < 4; i++) { if ( (c = pb.read()) == -1 ) { // Incomplete Unicode escape sequence at EOF. Just swallow // it. incomplete = true; break; } buf.append ((char) c); } if (incomplete) { // Push the entire buffered sequence back onto the input // stream. unread (buf.toString(), pb); } else { int code = 0; boolean error = false; try { code = Integer.parseInt (buf.toString(), 16); if (code < 0) throw new NumberFormatException(); } catch (NumberFormatException ex) { // Bad hexadecimal value in Unicode escape sequence. Push // it all back. unread (buf.toString(), pb); error = true; } if (! Character.isDefined ((char) code)) { // Invalid Unicode character. Push it all back. unread (buf.toString(), pb); error = true; } c = (error ? -2 : ((char) code)); } return c; }
Parse the next four characters and attempt to decode them as a Unicode character code. @param pb a PushbackReader representing the remainder of the region being processed (necessary for Unicode sequences) @return the decoded character, -1 on EOF, -2 for a bad Unicode sequence. If -2 is returned, the 4-character Unicode code is pushed back on the input stream. (The leading backslash and "u" are not pushed back, however). @throws IOException on error
decodeUnicodeSequence
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
private void unread (String s, PushbackReader pb) throws IOException { for (int i = s.length() - 1; i >= 0; i--) pb.unread (s.charAt (i)); }
Push a string back on the input stream. @param s the string @param pb the PushbackReader onto which to push the characters @throws IOException if an I/O error occurs or if the pushback buffer is full
unread
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBufBase.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBufBase.java
MIT
public void setLength (int newLength) throws IndexOutOfBoundsException { buf.setLength (newLength); }
Set the length of this <tt>XStringBuffer</tt>. This string buffer is altered to represent a new character sequence whose length is specified by the argument. If the <tt>newLength</tt> argument is less than the current length of the string buffer, the string buffer is truncated to contain exactly the number of characters given by the <tt>newLength</tt> argument. If the <tt>newLength</tt> argument is greater than the current length of the string buffer, the buffer is padded with null characters out to the new length. @param newLength the new length of the buffer @throws IndexOutOfBoundsException <tt>newLength</tt> is negative
setLength
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBuffer.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBuffer.java
MIT
public StringBuffer toStringBuffer() { return new StringBuffer (buf.toString()); }
Return a standard <tt>StringBuffer</tt> containing a copy of the contents of this buffer. @return The string buffer
toStringBuffer
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBuffer.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBuffer.java
MIT
protected Appendable getBufferAsAppendable() { return buf; }
Get the underlying buffer (e.g., <tt>StringBuffer</tt>, <tt>StringBuilder</tt>) as an <tt>Appender</tt> object. @return the <tt>Appender</tt>
getBufferAsAppendable
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBuffer.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBuffer.java
MIT
protected CharSequence newBufferAsCharSequence() { return new StringBuffer(); }
Get a new instance of the underlying buffer type (e.g., <tt>StringBuffer</tt>, <tt>StringBuilder</tt>) as a <tt>CharSequence</tt> object. @return the <tt>Appendable</tt>
newBufferAsCharSequence
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBuffer.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBuffer.java
MIT
protected CharSequence getBufferAsCharSequence() { return buf; }
Get the underlying buffer (e.g., <tt>StringBuffer</tt>, <tt>StringBuilder</tt>) as a <tt>CharSequence</tt> object. @return the <tt>Appendable</tt>
getBufferAsCharSequence
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBuffer.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBuffer.java
MIT
protected void deleteCharacters (int start, int end) throws IndexOutOfBoundsException { buf.delete (start, end); }
Remove the characters in a substring of this <tt>XStringBuffer</tt>. The substring begins at the specified <tt>start</tt> and extends to the character at index <tt>end - 1</tt>, or to the end of the string, if no such character exists. If <tt>start</tt> is equal to <tt>end</tt>, no changes are made. @param start The beginning index, inclusive @param end The ending index, exclusive @throws IndexOutOfBoundsException if <tt>start</tt> is negative, greater than <tt>length()</tt>, or greater than <tt>end</tt>
deleteCharacters
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBuffer.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBuffer.java
MIT
protected void replaceString (int start, int end, String str) throws IndexOutOfBoundsException { buf.replace (start, end, str); }
Replace the characters in a substring of this buffer with characters in the specified <tt>String</tt>. The substring begins at the specified <tt>start</tt> and extends to the character at index <tt>end - 1</tt>, or to the end of the <tt>XStringBuffer</tt> if no such character exists. First the characters in the substring are removed and then the specified <tt>String</tt> is inserted at <tt>start</tt>. (The <tt>XStringBuffer</tt> will be lengthened to accommodate the specified <tt>String</tt> if necessary.) @param start The beginning index, inclusive @param end The ending index, exclusive @param str The string that will replace the previous contents @throws IndexOutOfBoundsException if <tt>start</tt> is negative, or greater than <tt>length()</tt>, or greater than <tt>end</tt>
replaceString
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBuffer.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBuffer.java
MIT
public void trimToSize() { buf.trimToSize(); }
Attempts to reduce storage used for the character sequence. If the buffer is larger than necessary to hold its current sequence of characters, then it may be resized to become more space efficient. Calling this method may, but is not required to, affect the value returned by a subsequent call to the {@link #capacity} method.
trimToSize
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBuilder.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBuilder.java
MIT
protected CharSequence newBufferAsCharSequence() { return new StringBuilder(); }
Get a new instance of the underlying buffer type (e.g., <tt>StringBuilder</tt>, <tt>StringBuilder</tt>) as a <tt>CharSequence</tt> object. @return the <tt>Appendable</tt>
newBufferAsCharSequence
java
kiooeht/ModTheSpire
src/main/java/org/clapper/util/text/XStringBuilder.java
https://github.com/kiooeht/ModTheSpire/blob/master/src/main/java/org/clapper/util/text/XStringBuilder.java
MIT
@Override public void onEnterBuildable(Queue.BuildableItem item) { if (DISABLE_NODELAY_PROVISING) { return; } final Jenkins jenkins = Jenkins.get(); final Label label = item.getAssignedLabel(); for (Cloud cloud : jenkins.clouds) { if (cloud instanceof KubernetesCloud && cloud.canProvision(new Cloud.CloudState(label, 0))) { final NodeProvisioner provisioner = (label == null ? jenkins.unlabeledNodeProvisioner : label.nodeProvisioner); provisioner.suggestReviewNow(); } } }
Ping the nodeProvisioner as a new task enters the queue.
onEnterBuildable
java
jenkinsci/kubernetes-plugin
src/main/java/io/jenkins/plugins/kubernetes/NoDelayProvisionerStrategy.java
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/io/jenkins/plugins/kubernetes/NoDelayProvisionerStrategy.java
Apache-2.0
static KubernetesClient createClient(KubernetesCloud cloud) throws KubernetesAuthException, IOException { String displayName = cloud.getDisplayName(); final Client c = clients.getIfPresent(displayName); if (c == null) { KubernetesClient client = new KubernetesFactoryAdapter( cloud.getServerUrl(), cloud.getNamespace(), cloud.getServerCertificate(), cloud.getCredentialsId(), cloud.isSkipTlsVerify(), cloud.getConnectTimeout(), cloud.getReadTimeout(), cloud.getMaxRequestsPerHost(), cloud.isUseJenkinsProxy()) .createClient(); clients.put(displayName, new Client(getValidity(cloud), client)); LOGGER.log(Level.FINE, "Created new Kubernetes client: {0} {1}", new Object[] {displayName, client}); return client; } return c.getClient(); }
Client expiration in seconds. Some providers such as Amazon EKS use a token with 15 minutes expiration, so expire clients after 10 minutes.
createClient
java
jenkinsci/kubernetes-plugin
src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesClientProvider.java
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesClientProvider.java
Apache-2.0
@NonNull public String getJenkinsUrlOrDie() { String url = getJenkinsUrlOrNull(); if (url == null) { throw new IllegalStateException("Jenkins URL for Kubernetes is null"); } return url; }
@return same as {@link #getJenkinsUrlOrNull}, if set @throws IllegalStateException if no Jenkins URL could be computed.
getJenkinsUrlOrDie
java
jenkinsci/kubernetes-plugin
src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
Apache-2.0
@Deprecated public Map<String, String> getLabels() { return getPodLabelsMap(); }
Labels for all pods started by the plugin @return immutable map of pod labels @deprecated use {@link #getPodLabels()}
getLabels
java
jenkinsci/kubernetes-plugin
src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
Apache-2.0
@Deprecated public void setLabels(Map<String, String> labels) { setPodLabels(labels != null ? PodLabel.fromMap(labels) : Collections.emptyList()); }
Set pod labels @param labels pod labels @deprecated use {@link #setPodLabels(List)}
setLabels
java
jenkinsci/kubernetes-plugin
src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
Apache-2.0
@NonNull public List<PodLabel> getPodLabels() { return podLabels == null || podLabels.isEmpty() ? PodLabel.fromMap(DEFAULT_POD_LABELS) : podLabels; }
Labels for all pods started by the plugin
getPodLabels
java
jenkinsci/kubernetes-plugin
src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
Apache-2.0
@DataBoundSetter public void setPodLabels(@CheckForNull List<PodLabel> labels) { this.podLabels = new ArrayList<>(); if (labels != null) { this.podLabels.addAll(labels); } }
Set Pod labels for all pods started by the plugin.
setPodLabels
java
jenkinsci/kubernetes-plugin
src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
Apache-2.0
public PodRetention getPodRetention() { return this.podRetention; }
Gets the global pod retention policy for the plugin.
getPodRetention
java
jenkinsci/kubernetes-plugin
src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
Apache-2.0
@DataBoundSetter public void setPodRetention(PodRetention podRetention) { if (podRetention == null || podRetention instanceof Default) { podRetention = PodRetention.getKubernetesCloudDefault(); } this.podRetention = podRetention; }
Set the global pod retention policy for the plugin. @param podRetention the pod retention policy for the plugin.
setPodRetention
java
jenkinsci/kubernetes-plugin
src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
Apache-2.0
public KubernetesClient connect() throws KubernetesAuthException, IOException { LOGGER.log(Level.FINEST, "Building connection to Kubernetes {0} URL {1} namespace {2}", new String[] { getDisplayName(), serverUrl, namespace }); KubernetesClient client = KubernetesClientProvider.createClient(this); LOGGER.log(Level.FINE, "Connected to Kubernetes {0} URL {1} namespace {2}", new String[] { getDisplayName(), client.getMasterUrl().toString(), namespace }); return client; }
Connects to Kubernetes. @return Kubernetes client.
connect
java
jenkinsci/kubernetes-plugin
src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
Apache-2.0
@NonNull public PodResource getPodResource(@Nullable String namespace, @NonNull String name) throws KubernetesAuthException, IOException { return getPodResource(connect(), namespace, name); }
Get {@link PodResource} from {@link KubernetesClient}. @param namespace namespace pod is located in, possibly null @param name pod name, not null @return pod resource, never null @throws KubernetesAuthException if cluster authentication failed @throws IOException if connection failed @see #connect() @see #getPodResource(KubernetesClient, String, String)
getPodResource
java
jenkinsci/kubernetes-plugin
src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
Apache-2.0
private static void ensureKubernetesUrlInFipsMode(String url) { if (!FIPS140.useCompliantAlgorithms() || StringUtils.isBlank(url)) { return; } if (!url.startsWith("https:")) { throw new IllegalArgumentException(Messages.KubernetesCloud_kubernetesServerUrlIsNotSecure()); } }
Checks if URL is using HTTPS, required in FIPS mode Continues if URL is secure or not in FIPS mode, throws an {@link IllegalArgumentException} if not. @param url Kubernetes server URL
ensureKubernetesUrlInFipsMode
java
jenkinsci/kubernetes-plugin
src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
https://github.com/jenkinsci/kubernetes-plugin/blob/master/src/main/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloud.java
Apache-2.0