Example usage for com.google.common.base Strings padStart

List of usage examples for com.google.common.base Strings padStart

Introduction

In this page you can find the example usage for com.google.common.base Strings padStart.

Prototype

public static String padStart(String string, int minLength, char padChar) 

Source Link

Document

Returns a string, of length at least minLength , consisting of string prepended with as many copies of padChar as are necessary to reach that length.

Usage

From source file:com.facebook.buck.apple.ProjectGenerator.java

private String getFixUUIDShellScript(TargetNode<?> targetNode) {
    ST template;/*from   w  w  w . j a va 2  s  .  c  o  m*/
    try {
        template = new ST(Resources.toString(Resources.getResource(ProjectGenerator.class, FIX_UUID_TEMPLATE),
                Charsets.UTF_8));
    } catch (IOException e) {
        throw new RuntimeException("There was an error loading '" + FIX_UUID_TEMPLATE + "' template", e);
    }

    ImmutableSet<Flavor> flavors = ImmutableSet.copyOf(targetNode.getBuildTarget().getFlavors());
    CxxPlatform cxxPlatform = cxxPlatforms.getValue(flavors).or(defaultCxxPlatform);
    String compDir = cxxPlatform.getDebugPathSanitizer().getCompilationDirectory();
    // Use the hostname for padding instead of the directory, this way the directory matches without
    // having to resolve it.
    String sourceDir = Strings.padStart(":" + projectFilesystem.getRootPath().toString(), compDir.length(),
            'f');

    Optional<String> productName = getProductNameForTargetNode(targetNode);
    String binaryName = AppleBundle.getBinaryName(targetToBuildWithBuck.get(), productName);
    Path bundleDestination = getScratchPathForAppBundle(targetToBuildWithBuck.get(), binaryName);
    Path dsymDestination = getScratchPathForDsymBundle(targetToBuildWithBuck.get(), binaryName);
    Path resolvedBundleDestination = projectFilesystem.resolve(bundleDestination);
    Path resolvedDsymDestination = projectFilesystem.resolve(dsymDestination);
    Path fixUUIDScriptPath = getFixUUIDScriptPath(projectFilesystem);

    if (attemptToDetermineBestCxxPlatform) {
        template.add("buck_flavor", XCODE_BUILD_SCRIPT_FLAVOR_VALUE);
    } else {
        template.add("buck_flavor", "");
    }
    template.add("path_to_buck", getPathToBuck(executableFinder, environment));
    template.add("buck_target", targetToBuildWithBuck.get().getFullyQualifiedName());
    template.add("root_path", projectFilesystem.getRootPath());

    template.add("comp_dir", compDir);
    template.add("source_dir", sourceDir);

    template.add("resolved_bundle_destination", resolvedBundleDestination);
    template.add("resolved_bundle_destination_parent", resolvedBundleDestination.getParent());
    template.add("resolved_dsym_destination", resolvedDsymDestination);
    template.add("binary_name", binaryName);
    template.add("path_to_fix_uuid_script", fixUUIDScriptPath);

    return template.render();
}

From source file:com.facebook.buck.apple.project_generator.ProjectGenerator.java

private String getBuildWithBuckShellScript(TargetNode<?, ?> targetNode) {
    ST template;//  ww w.  j a v a  2 s  . c o  m
    try {
        template = new ST(Resources.toString(
                Resources.getResource(ProjectGenerator.class, BUILD_WITH_BUCK_TEMPLATE), Charsets.UTF_8));
    } catch (IOException e) {
        throw new RuntimeException("There was an error loading '" + BUILD_WITH_BUCK_TEMPLATE + "' template", e);
    }

    String buildFlags = getBuildFlags();
    String escapedBuildTarget = Escaper.escapeAsBashString(targetNode.getBuildTarget().getFullyQualifiedName());

    Optional<String> productName = getProductNameForTargetNode(targetNode);
    String binaryName = AppleBundle.getBinaryName(targetNode.getBuildTarget(), productName);
    Path bundleDestination = getScratchPathForAppBundle(projectFilesystem, targetNode.getBuildTarget(),
            binaryName);
    Path dsymDestination = getScratchPathForDsymBundle(projectFilesystem, targetNode.getBuildTarget(),
            binaryName);
    Path resolvedBundleDestination = projectFilesystem.resolve(bundleDestination);
    Path resolvedDsymDestination = projectFilesystem.resolve(dsymDestination);

    ImmutableSet<Flavor> flavors = ImmutableSet.copyOf(targetNode.getBuildTarget().getFlavors());
    CxxPlatform cxxPlatform = cxxPlatforms.getValue(flavors).orElse(defaultCxxPlatform);
    String oldCompDir = cxxPlatform.getCompilerDebugPathSanitizer().getCompilationDirectory();
    // Use the hostname for padding instead of the directory, this way the directory matches without
    // having to resolve it.
    String dsymPaddedCompDirWithHost = Strings.padStart(":" + projectFilesystem.getRootPath().toString(),
            oldCompDir.length(), 'f');

    template.add("path_to_build_with_buck_py", getBuildWithBuckPythonScriptPath(projectFilesystem));
    template.add("path_to_fix_uuid_script", getFixUUIDScriptPath(projectFilesystem));
    template.add("repo_root", projectFilesystem.getRootPath());
    template.add("path_to_buck", getPathToBuck(executableFinder, environment));
    template.add("build_flags", buildFlags);
    template.add("escaped_build_target", escapedBuildTarget);
    template.add("buck_dwarf_flavor",
            (appleConfig.forceDsymModeInBuildWithBuck() ? AppleDebugFormat.DWARF_AND_DSYM
                    : AppleDebugFormat.DWARF).getFlavor().getName());
    template.add("buck_dsym_flavor", AppleDebugFormat.DWARF_AND_DSYM.getFlavor().getName());
    template.add("binary_name", binaryName);
    template.add("comp_dir", oldCompDir);
    template.add("new_comp_dir", projectFilesystem.getRootPath().toString());
    template.add("padded_source_dir", dsymPaddedCompDirWithHost);
    template.add("resolved_bundle_destination", resolvedBundleDestination);
    template.add("resolved_bundle_destination_parent", resolvedBundleDestination.getParent());
    template.add("resolved_dsym_destination", resolvedDsymDestination);
    template.add("force_dsym", appleConfig.forceDsymModeInBuildWithBuck() ? "true" : "false");

    return template.render();
}

From source file:com.facebook.buck.parser.PythonDslProjectBuildFileParser.java

@SuppressWarnings("unchecked")
private static IOException createParseException(Path buildFile, Path buckPyDir, String message,
        @Nullable Object exception) {
    if (!(exception instanceof Map<?, ?>)) {
        return new IOException(message);
    } else {/*from  w ww  .  j a v  a2 s  .co  m*/
        Map<String, Object> exceptionMap = (Map<String, Object>) exception;
        BuildFileParseExceptionData exceptionData = parseExceptionData(exceptionMap);
        LOG.debug("Received exception from buck.py parser: %s", exceptionData);
        Optional<BuildFileSyntaxError> syntaxErrorOpt = exceptionData.getSyntaxError();
        if (syntaxErrorOpt.isPresent()) {
            BuildFileSyntaxError syntaxError = syntaxErrorOpt.get();
            String errorMsg = "";
            if (buildFile.equals(syntaxError.getFileName())) {
                // BuildFileParseException will include the filename
                errorMsg += String.format("Syntax error on line %s", syntaxError.getLineNumber());
            } else {
                // Parse error was in some other file included by the build file
                errorMsg += String.format("Syntax error in %s\nLine %s", syntaxError.getFileName(),
                        syntaxError.getLineNumber());
            }
            if (syntaxError.getOffset().isPresent()) {
                errorMsg += String.format(", column %s", syntaxError.getOffset().get());
            }
            errorMsg += ":\n" + syntaxError.getText();
            if (syntaxError.getOffset().isPresent()) {
                errorMsg += Strings.padStart("^", syntaxError.getOffset().get().intValue(), ' ');
            }
            return new IOException(errorMsg);
        } else if (exceptionData.getType().equals("IncorrectArgumentsException")) {
            return new IOException(message);
        } else {
            String formattedStackTrace = formatStackTrace(buckPyDir, exceptionData.getStackTrace());
            return new IOException(String.format("%s: %s\nCall stack:\n%s", exceptionData.getType(),
                    exceptionData.getValue(), formattedStackTrace));
        }
    }
}

From source file:com.zimbra.cs.service.formatter.ArchiveFormatter.java

/**
 * Get entry name. If prefix is true guarantee uniqueness by prepending itemId. If prefix is false caller must guarantee uniqueness
 *///from www.j  av a2  s.  c o  m
private String getEntryName(MailItem mi, String fldr, String name, String ext, CharsetEncoder encoder,
        boolean prefix) {
    String path;

    if (Strings.isNullOrEmpty(name)) {
        name = mi.getName();
    }
    if (Strings.isNullOrEmpty(name)) {
        name = mi.getSubject();
    }
    if (prefix && !Strings.isNullOrEmpty(name)) {
        name = Strings.padStart(mi.getId() + "", 10, '0') + "-" + sanitize(name, encoder);
    }
    if (Strings.isNullOrEmpty(name)) {
        name = mi.getType().toString() + '-' + mi.getId();
    } else if (name.length() > 121) {
        name = name.substring(0, 120);
    }
    if (mi.isTagged(Flag.FlagInfo.VERSIONED)) {
        // prepend the version before the extension of up to four characters
        int dot = name.lastIndexOf('.');
        if (dot >= 0 && dot >= name.length() - 5) {
            name = name.substring(0, dot) + String.format("-%05d", mi.getVersion()) + name.substring(dot);
        } else {
            name += String.format("-%05d", mi.getVersion());
        }
    }
    name = ILLEGAL_FILE_CHARS.matcher(name).replaceAll("_").trim();
    while (name.endsWith(".")) {
        name = name.substring(0, name.length() - 1).trim();
    }
    path = fldr.equals("") ? name : fldr + '/' + name;
    if (ext != null) {
        path += '.' + ext;
    }
    return path;
}

From source file:com.addthis.basis.util.LessBytes.java

/**
 * Pad the String representation of a long with leading zeros.
 *
 * @deprecated Use either {@link String#format(String, Object...)} or
 *     {@link Strings#padStart(String, int, char)}. Also, this has little
 *     to do with Bytes./*from  ww w .j  ava 2 s  .  com*/
 */
@Deprecated
public static String pad0(long val, int zeros) {
    String sval = Long.toString(val);
    return Strings.padStart(sval, zeros, '0');
}