Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright 2014, Google Inc.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are
      7  * met:
      8  *
      9  *     * Redistributions of source code must retain the above copyright
     10  * notice, this list of conditions and the following disclaimer.
     11  *     * Redistributions in binary form must reproduce the above
     12  * copyright notice, this list of conditions and the following disclaimer
     13  * in the documentation and/or other materials provided with the
     14  * distribution.
     15  *     * Neither the name of Google Inc. nor the names of its
     16  * contributors may be used to endorse or promote products derived from
     17  * this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 package org.jf.util;
     33 
     34 import javax.annotation.Nonnull;
     35 import java.util.regex.Matcher;
     36 import java.util.regex.Pattern;
     37 
     38 public class TextUtils {
     39     private static String newline = System.getProperty("line.separator");
     40 
     41     @Nonnull
     42     public static String normalizeNewlines(@Nonnull String source) {
     43         return normalizeNewlines(source, newline);
     44     }
     45 
     46     @Nonnull
     47     public static String normalizeNewlines(@Nonnull String source, String newlineValue) {
     48         return source.replace("\r", "").replace("\n", newlineValue);
     49     }
     50 
     51     @Nonnull
     52     public static String normalizeWhitespace(@Nonnull String source) {
     53         // Go to native system new lines so that ^/$ work correctly
     54         source = normalizeNewlines(source);
     55 
     56         // Remove all suffix/prefix whitespace
     57         Pattern pattern = Pattern.compile("((^[ \t]+)|([ \t]+$))", Pattern.MULTILINE);
     58         Matcher matcher = pattern.matcher(source);
     59         source = matcher.replaceAll("");
     60 
     61         // Remove all empty lines
     62         Pattern pattern2 = Pattern.compile("^\r?\n?", Pattern.MULTILINE);
     63         Matcher matcher2 = pattern2.matcher(source);
     64         source = matcher2.replaceAll("");
     65 
     66         // Remove a trailing new line, if present
     67         Pattern pattern3 = Pattern.compile("\r?\n?$");
     68         Matcher matcher3 = pattern3.matcher(source);
     69         source = matcher3.replaceAll("");
     70 
     71         // Go back to unix-style \n newlines
     72         source = normalizeNewlines(source, "\n");
     73         return source;
     74     }
     75 
     76     @Nonnull
     77     public static String stripComments(@Nonnull String source) {
     78         Pattern pattern = Pattern.compile("#(.*)");
     79         Matcher matcher = pattern.matcher(source);
     80         return matcher.replaceAll("");
     81     }
     82 }
     83