001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.uon;
014
015import static org.apache.juneau.internal.StringUtils.*;
016
017import org.apache.juneau.internal.*;
018
019/**
020 * Utility methods for the UON and UrlEncoding serializers and parsers.
021 */
022public final class UonUtils {
023
024   private static final AsciiSet needsQuoteChars = AsciiSet.create("),=\n\t\r\b\f ");
025   private static final AsciiSet maybeNeedsQuotesFirstChar = AsciiSet.create("),=\n\t\r\b\f tfn+-.#0123456789");
026
027   /**
028    * Returns <jk>true</jk> if the specified string needs to be quoted per UON notation.
029    *
030    * <p>
031    * For example, strings that start with '(' or '@' or look like boolean or numeric values need to be quoted.
032    *
033    * @param s The string to test.
034    * @return <jk>true</jk> if the specified string needs to be quoted per UON notation.
035    */
036   public static final boolean needsQuotes(String s) {
037      char c0 = s.isEmpty() ? 0 : s.charAt(0);
038      return (
039         s.isEmpty()
040         || c0 == '@'
041         || c0 == '('
042         || needsQuoteChars.contains(s)
043         || (
044            maybeNeedsQuotesFirstChar.contains(c0)
045            && (
046               "true".equals(s)
047               || "false".equals(s)
048               || "null".equals(s)
049               || isNumeric(s)
050            )
051         )
052      );
053   }
054}