1/*
2 * Copyright (C) 2014 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 */
16package android.alsa;
17
18/**
19 * @hide
20 * Breaks lines in an ALSA "cards" or "devices" file into tokens.
21 * TODO(pmclean) Look into replacing this with String.split().
22 */
23public class LineTokenizer {
24    public static final int kTokenNotFound = -1;
25
26    private String mDelimiters = "";
27
28    public LineTokenizer(String delimiters) {
29        mDelimiters = delimiters;
30    }
31
32    int nextToken(String line, int startIndex) {
33        int len = line.length();
34        int offset = startIndex;
35        for (; offset < len; offset++) {
36            if (mDelimiters.indexOf(line.charAt(offset)) == -1) {
37                // past a delimiter
38                break;
39            }
40      }
41
42      return offset < len ? offset : kTokenNotFound;
43    }
44
45    int nextDelimiter(String line, int startIndex) {
46        int len = line.length();
47        int offset = startIndex;
48        for (; offset < len; offset++) {
49            if (mDelimiters.indexOf(line.charAt(offset)) != -1) {
50                // past a delimiter
51                break;
52            }
53        }
54
55      return offset < len ? offset : kTokenNotFound;
56    }
57}
58