下面列出了com.google.common.base.Strings#padStart ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns a StringColumn with the year and week-of-year derived from this column concatenated
* into a String that will sort lexicographically in temporal order.
*
* <p>This simplifies the production of plots and tables that aggregate values into standard
* temporal units (e.g., you want monthly data but your source data is more than a year long and
* you don't want months from different years aggregated together).
*/
default StringColumn yearWeek() {
StringColumn newColumn = StringColumn.create(this.name() + " year & month");
for (int r = 0; r < this.size(); r++) {
int c1 = this.getIntInternal(r);
if (DateColumn.valueIsMissing(c1)) {
newColumn.appendMissing();
} else {
String ym = String.valueOf(PackedLocalDate.getYear(c1));
ym = ym + "-" + Strings.padStart(String.valueOf(PackedLocalDate.getWeekOfYear(c1)), 2, '0');
newColumn.append(ym);
}
}
return newColumn;
}
protected void log(String text, long start, Integer count) {
long end = System.currentTimeMillis();
String elapsed = ((end - start) / 1000) + "s";
String token2 = "";
String token3;
if (count == null) {
token3 = Strings.padStart(" " + elapsed, 35 - text.length(), ' ');
} else {
token2 = Strings.padStart(" " + count.toString(), 25 - text.length(), ' ');
token3 = Strings.padStart(" " + elapsed, 10, ' ');
}
LOGGER.info(text + token2 + token3);
}
@Test
public void left_pad_string_with_zeros_guava () {
String leftPaddedString = Strings.padStart("levelup", 10, '0');
assertEquals("000levelup", leftPaddedString);
assertEquals(10, leftPaddedString.length());
assertThat(leftPaddedString, startsWith("0"));
}
public String adjust(String shardHash) {
HashCode hashCode = Hashing.md5().hashBytes(shardHash.getBytes());
String binary =
Strings.padStart(new BigInteger(1, hashCode.asBytes()).toString(2), BINARY_LENGTH, '0');
String adjustedBinary = Strings.padEnd(binary.substring(0, reservedBits), BINARY_LENGTH, '0');
return Strings.padStart(new BigInteger(adjustedBinary, 2).toString(16), HEX_LENGTH, '0');
}
/**
* Creates lexer rule for given keyword in order to handle JavaScript's unicode masks. E.g.:
*
* <pre>
* If :
* ( 'i' | '\\' 'u' '0''0''6''9' )
* ( 'f' | '\\' 'u' '0''0''6''6' );
* </pre>
*
* @param keyword
* the keyword as string, e.g. 'if'
* @return the lexer body, e.g.
*
* <pre>
* ( 'i' | '\\' 'u' '0''0''6''9' ) ( 'f' | '\\' 'u' '0''0''6''6' )
* </pre>
*/
public static String toUnicodeKeyword(String keyword) {
if (keyword.equals("async ")) {
keyword = "async";
}
if (isIdentifier(keyword)) {
StringBuilder result = new StringBuilder(keyword.length() * 30);
for (char c : keyword.toCharArray()) {
result.append("\n\t( '");
result.append(c);
result.append("' | '\\\\' 'u' ");
String unicodeEscape = Strings.padStart(Integer.toHexString(c), 4, '0');
for (char u : unicodeEscape.toCharArray()) {
if ('0' <= u && u <= '9') {
result.append("'");
result.append(u);
result.append("'");
} else {
result.append("( '");
result.append(u);
result.append("' | '");
result.append(Character.toUpperCase(u));
result.append("' )");
}
}
result.append(" )");
}
return result.toString();
}
return "'" + AntlrGrammarGenUtil.toAntlrString(keyword) + "'";
}
/**
* Returns a StringColumn with the year and month from this column concatenated into a String that
* will sort lexicographically in temporal order.
*
* <p>This simplifies the production of plots and tables that aggregate values into standard
* temporal units (e.g., you want monthly data but your source data is more than a year long and
* you don't want months from different years aggregated together).
*/
default StringColumn yearMonth() {
StringColumn newColumn = StringColumn.create(this.name() + " year & month");
for (int r = 0; r < this.size(); r++) {
long c1 = this.getLongInternal(r);
if (DateTimeColumn.valueIsMissing(c1)) {
newColumn.append(StringColumnType.missingValueIndicator());
} else {
String ym = String.valueOf(getYear(c1));
ym = ym + "-" + Strings.padStart(String.valueOf(getMonthValue(c1)), 2, '0');
newColumn.append(ym);
}
}
return newColumn;
}
public static String toDateString(int date) {
if (date == Integer.MIN_VALUE) {
return "";
}
return getYear(date)
+ "-"
+ Strings.padStart(Byte.toString(getMonthValue(date)), 2, '0')
+ "-"
+ Strings.padStart(Byte.toString(getDayOfMonth(date)), 2, '0');
}
/**
* Returns a StringColumn with the year and quarter from this column concatenated into a String
* that will sort lexicographically in temporal order.
*
* <p>This simplifies the production of plots and tables that aggregate values into standard
* temporal units (e.g., you want monthly data but your source data is more than a year long and
* you don't want months from different years aggregated together).
*/
default StringColumn yearQuarter() {
StringColumn newColumn = StringColumn.create(this.name() + " year & quarter");
for (int r = 0; r < this.size(); r++) {
int c1 = this.getIntInternal(r);
if (DateColumn.valueIsMissing(c1)) {
newColumn.appendMissing();
} else {
String yq = String.valueOf(PackedLocalDate.getYear(c1));
yq = yq + "-" + Strings.padStart(String.valueOf(PackedLocalDate.getQuarter(c1)), 2, '0');
newColumn.append(yq);
}
}
return newColumn;
}
/**
* Returns a StringColumn with the year and month from this column concatenated into a String that
* will sort lexicographically in temporal order.
*
* <p>This simplifies the production of plots and tables that aggregate values into standard
* temporal units (e.g., you want monthly data but your source data is more than a year long and
* you don't want months from different years aggregated together).
*/
default StringColumn yearMonth() {
StringColumn newColumn = StringColumn.create(this.name() + " year & month");
for (int r = 0; r < this.size(); r++) {
int c1 = this.getIntInternal(r);
if (DateColumn.valueIsMissing(c1)) {
newColumn.appendMissing();
} else {
String ym = String.valueOf(PackedLocalDate.getYear(c1));
ym = ym + "-" + Strings.padStart(String.valueOf(PackedLocalDate.getMonthValue(c1)), 2, '0');
newColumn.append(ym);
}
}
return newColumn;
}
/**
* Returns the Time-based One-time Password value against an arbitrary
* {@code time} using the set of parameters configured in this builder. The
* value will contain {@link #digits(int)} digits.
*
* @param time
* the time (in milliseconds)
*
* @return the Time-based One-time Password value as numeric String in base
* 10 that includes {@link #digits(int)} digits.
*/
private String generateTOTP(long time) {
// Calculate the number of time steps between the initial counter time
// (i.e. T0 = 0 = Unix epoch) and the specified 'time'.
final long tc = (long) Math.floor(time / timeStep);
// Using the counter
// First 8 bytes are for the movingFactor
// Compliant with base RFC 4226 (HOTP)
String timeInHex = Strings.padStart(Long.toHexString(tc).toUpperCase(), 16, '0');
// Step 1: Generate the HMAC-SHA hash.
byte[] msg = BaseEncoding.base16().decode(timeInHex);
byte[] hash = computeHmacSha(key, msg);
// Step 2: Dynamic Truncation as per section 5.3 of RFC 4226.
// -
// "... Let OffsetBits be the low-order 4 bits of String[19] (where String = String[0]...String[19]) ..."
// -
// "... Let P = String[OffSet]...String[OffSet+3] ... Return the Last 31 bits of P ..."
int offset = hash[hash.length - 1] & 0xf;
int binary = ((hash[offset] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16) | ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff);
// Step 3: Compute the TOTP value.
int otp = binary % ((int) Math.pow(10, digits));
// Ensure the TOTP value contains the specified number of digits.
return Strings.padStart(Integer.toString(otp), digits, '0');
}
/**
* Returns a StringColumn with the year and week-of-year derived from this column concatenated
* into a String that will sort lexicographically in temporal order.
*
* <p>This simplifies the production of plots and tables that aggregate values into standard
* temporal units (e.g., you want monthly data but your source data is more than a year long and
* you don't want months from different years aggregated together).
*/
default StringColumn yearWeek() {
StringColumn newColumn = StringColumn.create(this.name() + " year & month");
for (int r = 0; r < this.size(); r++) {
int c1 = this.getIntInternal(r);
if (DateColumn.valueIsMissing(c1)) {
newColumn.appendMissing();
} else {
String ym = String.valueOf(PackedLocalDate.getYear(c1));
ym = ym + "-" + Strings.padStart(String.valueOf(PackedLocalDate.getWeekOfYear(c1)), 2, '0');
newColumn.append(ym);
}
}
return newColumn;
}
/**
* Returns a StringColumn with the year and day-of-year derived from this column concatenated into
* a String that will sort lexicographically in temporal order.
*
* <p>This simplifies the production of plots and tables that aggregate values into standard
* temporal units (e.g., you want monthly data but your source data is more than a year long and
* you don't want months from different years aggregated together).
*/
default StringColumn yearDay() {
StringColumn newColumn = StringColumn.create(this.name() + " year & month");
for (int r = 0; r < this.size(); r++) {
int c1 = this.getIntInternal(r);
if (DateColumn.valueIsMissing(c1)) {
newColumn.appendMissing();
} else {
String ym = String.valueOf(PackedLocalDate.getYear(c1));
ym = ym + "-" + Strings.padStart(String.valueOf(PackedLocalDate.getDayOfYear(c1)), 3, '0');
newColumn.append(ym);
}
}
return newColumn;
}
/**
* Returns a StringColumn with the hour and minute-of-hour derived from this column concatenated
* into a String that will sort lexicographically in temporal order.
*
* <p>This simplifies the production of plots and tables that aggregate values into standard
* temporal units
*/
default StringColumn hourMinute() {
StringColumn newColumn = StringColumn.create(this.name() + " hour & minute");
for (int r = 0; r < this.size(); r++) {
int c1 = this.getIntInternal(r);
if (TimeColumn.valueIsMissing(c1)) {
newColumn.append(StringColumnType.missingValueIndicator());
} else {
String hm = Strings.padStart(String.valueOf(PackedLocalTime.getHour(c1)), 2, '0');
hm = hm + "-" + Strings.padStart(String.valueOf(PackedLocalTime.getMinute(c1)), 2, '0');
newColumn.append(hm);
}
}
return newColumn;
}
/**
* Returns a StringColumn with the year and week-of-year derived from this column concatenated
* into a String that will sort lexicographically in temporal order.
*
* <p>This simplifies the production of plots and tables that aggregate values into standard
* temporal units (e.g., you want monthly data but your source data is more than a year long and
* you don't want months from different years aggregated together).
*/
default StringColumn yearWeek() {
StringColumn newColumn = StringColumn.create(this.name() + " year & month");
for (int r = 0; r < this.size(); r++) {
long c1 = this.getLongInternal(r);
if (DateTimeColumn.valueIsMissing(c1)) {
newColumn.append(StringColumnType.missingValueIndicator());
} else {
String ym = String.valueOf(getYear(c1));
ym = ym + "-" + Strings.padStart(String.valueOf(getWeekOfYear(c1)), 2, '0');
newColumn.append(ym);
}
}
return newColumn;
}
/**
* Returns a StringColumn with the year and week-of-year derived from this column concatenated
* into a String that will sort lexicographically in temporal order.
*
* <p>This simplifies the production of plots and tables that aggregate values into standard
* temporal units (e.g., you want monthly data but your source data is more than a year long and
* you don't want months from different years aggregated together).
*/
default StringColumn hourMinute() {
StringColumn newColumn = StringColumn.create(this.name() + " hour & minute");
for (int r = 0; r < this.size(); r++) {
long c1 = this.getLongInternal(r);
if (DateTimeColumn.valueIsMissing(c1)) {
newColumn.append(StringColumnType.missingValueIndicator());
} else {
String hm = Strings.padStart(String.valueOf(getHour(c1)), 2, '0');
hm = hm + ":" + Strings.padStart(String.valueOf(getMinute(c1)), 2, '0');
newColumn.append(hm);
}
}
return newColumn;
}
private static String toHex(long num) {
return Strings.padStart(Long.toHexString(num), 16, '0');
}
private static void putSpaceLeftPaddedString(ByteBuffer buffer, int len, String value) {
Preconditions.checkState(value.length() <= len);
value = Strings.padStart(value, len, ' ');
buffer.put(value.getBytes(Charsets.US_ASCII));
}
@VisibleForTesting static String prependedPrefix(String prefix) {
return Strings.padStart(prefix, MIN_LENGTH, PAD_CHAR);
}
/**
* Converts a long value to hex string; 16 wide and sans 0x.
*
* @param value
* long value
* @param width
* string width; zero padded
* @return hex string
*/
public static String toHex(long value, int width) {
return Strings.padStart(UnsignedLongs.toString(value, 16), width, '0');
}
/**
* Converts a long value to hex string; 16 wide and sans 0x.
*
* @param value
* long value
* @return hex string
*/
public static String toHex(long value) {
return Strings.padStart(UnsignedLongs.toString(value, 16), 16, '0');
}