1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.squareup.okhttp.internal.http;
18
19public final class HeaderParser {
20  /**
21   * Returns the next index in {@code input} at or after {@code pos} that
22   * contains a character from {@code characters}. Returns the input length if
23   * none of the requested characters can be found.
24   */
25  public static int skipUntil(String input, int pos, String characters) {
26    for (; pos < input.length(); pos++) {
27      if (characters.indexOf(input.charAt(pos)) != -1) {
28        break;
29      }
30    }
31    return pos;
32  }
33
34  /**
35   * Returns the next non-whitespace character in {@code input} that is white
36   * space. Result is undefined if input contains newline characters.
37   */
38  public static int skipWhitespace(String input, int pos) {
39    for (; pos < input.length(); pos++) {
40      char c = input.charAt(pos);
41      if (c != ' ' && c != '\t') {
42        break;
43      }
44    }
45    return pos;
46  }
47
48  /**
49   * Returns {@code value} as a positive integer, or 0 if it is negative, or
50   * -1 if it cannot be parsed.
51   */
52  public static int parseSeconds(String value) {
53    try {
54      long seconds = Long.parseLong(value);
55      if (seconds > Integer.MAX_VALUE) {
56        return Integer.MAX_VALUE;
57      } else if (seconds < 0) {
58        return 0;
59      } else {
60        return (int) seconds;
61      }
62    } catch (NumberFormatException e) {
63      return -1;
64    }
65  }
66
67  private HeaderParser() {
68  }
69}
70