1 /**
2  * Copyright (c) 2008, http://www.snakeyaml.org
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 package org.yaml.snakeyaml.scanner;
17
18 import java.util.Arrays;
19
20 public final class Constant {
21     private final static String ALPHA_S = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-_";
22
23     private final static String LINEBR_S = "\n\u0085\u2028\u2029";
24     private final static String FULL_LINEBR_S = "\r" + LINEBR_S;
25     private final static String NULL_OR_LINEBR_S = "\0" + FULL_LINEBR_S;
26     private final static String NULL_BL_LINEBR_S = " " + NULL_OR_LINEBR_S;
27     private final static String NULL_BL_T_LINEBR_S = "\t" + NULL_BL_LINEBR_S;
28     private final static String NULL_BL_T_S = "\0 \t";
29     private final static String URI_CHARS_S = ALPHA_S + "-;/?:@&=+$,_.!~*\'()[]%";
30
31     public final static Constant LINEBR = new Constant(LINEBR_S);
32     public final static Constant FULL_LINEBR = new Constant(FULL_LINEBR_S);
33     public final static Constant NULL_OR_LINEBR = new Constant(NULL_OR_LINEBR_S);
34     public final static Constant NULL_BL_LINEBR = new Constant(NULL_BL_LINEBR_S);
35     public final static Constant NULL_BL_T_LINEBR = new Constant(NULL_BL_T_LINEBR_S);
36     public final static Constant NULL_BL_T = new Constant(NULL_BL_T_S);
37     public final static Constant URI_CHARS = new Constant(URI_CHARS_S);
38
39     public final static Constant ALPHA = new Constant(ALPHA_S);
40
41     private String content;
42     boolean[] contains = new boolean[128];
43     boolean noASCII = false;
44
45     private Constant(String content) {
46         Arrays.fill(contains, false);
47         StringBuilder sb = new StringBuilder();
48         for (int i = 0; i < content.length(); i++) {
49             int c = content.codePointAt(i);
50             if (c < 128)
51                 contains[c] = true;
52             else
53                 sb.appendCodePoint(c);
54         }
55         if (sb.length() > 0) {
56             noASCII = true;
57             this.content = sb.toString();
58         }
59     }
60
61     public boolean has(int c) {
62         return (c < 128) ? contains[c] : noASCII && content.indexOf(c, 0) != -1;
63     }
64
65     public boolean hasNo(int c) {
66         return !has(c);
67     }
68
69     public boolean has(int c, String additional) {
70         return has(c) || additional.indexOf(c, 0) != -1;
71     }
72
73     public boolean hasNo(int c, String additional) {
74         return !has(c, additional);
75     }
76 }
77