Android ASCII Check containsOnlyNonCrLfPrintableAscii( final Collection values)

Here you can find the source of containsOnlyNonCrLfPrintableAscii( final Collection values)

Description

contains Only Non Cr Lf Printable Ascii

License

Open Source License

Declaration

public static boolean containsOnlyNonCrLfPrintableAscii(
            final Collection<String> values) 

Method Source Code

//package com.java2s;
/*//from   w  ww  .java  2 s .co  m
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.text.TextUtils;

import java.util.Arrays;
import java.util.Collection;

public class Main {
    /**
     * <p>
     * This is useful when checking the string should be encoded into quoted-printable or not, which is required by vCard 2.1.
     * </p>
     * <p>
     * See the definition of "7bit" in vCard 2.1 spec for more information.
     * </p>
     */
    public static boolean containsOnlyNonCrLfPrintableAscii(
            final String... values) {
        if (values == null) {
            return true;
        }
        return containsOnlyNonCrLfPrintableAscii(Arrays.asList(values));
    }

    public static boolean containsOnlyNonCrLfPrintableAscii(
            final Collection<String> values) {
        if (values == null) {
            return true;
        }
        final int asciiFirst = 0x20;
        final int asciiLast = 0x7E; // included
        for (final String value : values) {
            if (TextUtils.isEmpty(value)) {
                continue;
            }
            final int length = value.length();
            for (int i = 0; i < length; i = value.offsetByCodePoints(i, 1)) {
                final int c = value.codePointAt(i);
                if (!(asciiFirst <= c && c <= asciiLast)) {
                    return false;
                }
            }
        }
        return true;
    }
}

Related

  1. containsOnlyNonCrLfPrintableAscii(String s)
  2. containsOnlyPrintableAscii(String s)
  3. isPureAscii(String v)
  4. containsOnlyNonCrLfPrintableAscii( final String... values)
  5. checkAscii(char c)
  6. isPrintableAscii(int c)