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 
Constant(String content)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             char ch = content.charAt(i);
50             if (ch < 128)
51                 contains[ch] = true;
52             else
53                 sb.append(ch);
54         }
55         if (sb.length() > 0) {
56             noASCII = true;
57             this.content = sb.toString();
58         }
59     }
60 
has(char ch)61     public boolean has(char ch) {
62         return (ch < 128) ? contains[ch] : noASCII && content.indexOf(ch, 0) != -1;
63     }
64 
hasNo(char ch)65     public boolean hasNo(char ch) {
66         return !has(ch);
67     }
68 
has(char ch, String additional)69     public boolean has(char ch, String additional) {
70         return has(ch) || additional.indexOf(ch, 0) != -1;
71     }
72 
hasNo(char ch, String additional)73     public boolean hasNo(char ch, String additional) {
74         return !has(ch, additional);
75     }
76 }
77