Example usage for org.eclipse.jgit.util RawParseUtils parseTimeZoneOffset

List of usage examples for org.eclipse.jgit.util RawParseUtils parseTimeZoneOffset

Introduction

In this page you can find the example usage for org.eclipse.jgit.util RawParseUtils parseTimeZoneOffset.

Prototype

public static final int parseTimeZoneOffset(byte[] b, int ptr) 

Source Link

Document

Parse a Git style timezone string.

Usage

From source file:playRepository.GitCommit.java

License:Apache License

/**
 * Parse a name line (e.g. author, committer, tagger) into a PersonIdent.
 * <p>// w w  w. ja  va  2s  . c  o m
 * When passing in a value for <code>nameB</code> callers should use the
 * return value of {@link RawParseUtils#author(byte[], int)} or
 * {@link RawParseUtils#committer(byte[], int)}, as these methods provide the proper
 * position within the buffer.
 *
 * @param raw
 *            the buffer to parse character data from.
 * @param nameB
 *            first position of the identity information. This should be the
 *            first position after the space which delimits the header field
 *            name (e.g. "author" or "committer") from the rest of the
 *            identity line.
 * @return the parsed identity or null in case the identity could not be
 *         parsed.
 */
public static PersonIdent parsePersonIdent(final byte[] raw, final int nameB, Charset fallback) {
    // This line and the third parameter of this method is added by Yi EungJun. 2013/12/10
    final Charset cs = (fallback == null) ? RawParseUtils.parseEncoding(raw) : parseEncoding(raw, fallback);

    final int emailB = RawParseUtils.nextLF(raw, nameB, '<');
    final int emailE = RawParseUtils.nextLF(raw, emailB, '>');
    if (emailB >= raw.length || raw[emailB] == '\n' || (emailE >= raw.length - 1 && raw[emailE - 1] != '>'))
        return null;

    final int nameEnd = emailB - 2 >= nameB && raw[emailB - 2] == ' ' ? emailB - 2 : emailB - 1;
    final String name = RawParseUtils.decode(cs, raw, nameB, nameEnd);
    final String email = RawParseUtils.decode(cs, raw, emailB, emailE - 1);

    // Start searching from end of line, as after first name-email pair,
    // another name-email pair may occur. We will ignore all kinds of
    // "junk" following the first email.
    //
    // We've to use (emailE - 1) for the case that raw[email] is LF,
    // otherwise we would run too far. "-2" is necessary to position
    // before the LF in case of LF termination resp. the penultimate
    // character if there is no trailing LF.
    final int tzBegin = lastIndexOfTrim(raw, ' ', RawParseUtils.nextLF(raw, emailE - 1) - 2) + 1;
    if (tzBegin <= emailE) // No time/zone, still valid
        return new PersonIdent(name, email, 0, 0);

    final int whenBegin = Math.max(emailE, lastIndexOfTrim(raw, ' ', tzBegin - 1) + 1);
    if (whenBegin >= tzBegin - 1) // No time/zone, still valid
        return new PersonIdent(name, email, 0, 0);

    final long when = RawParseUtils.parseLongBase10(raw, whenBegin, null);
    final int tz = RawParseUtils.parseTimeZoneOffset(raw, tzBegin);
    return new PersonIdent(name, email, when * 1000L, tz);
}