001//////////////////////////////////////////////////////////////////////////////// 002// checkstyle: Checks Java source code for adherence to a set of rules. 003// Copyright (C) 2001-2017 the original author or authors. 004// 005// This library is free software; you can redistribute it and/or 006// modify it under the terms of the GNU Lesser General Public 007// License as published by the Free Software Foundation; either 008// version 2.1 of the License, or (at your option) any later version. 009// 010// This library is distributed in the hope that it will be useful, 011// but WITHOUT ANY WARRANTY; without even the implied warranty of 012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013// Lesser General Public License for more details. 014// 015// You should have received a copy of the GNU Lesser General Public 016// License along with this library; if not, write to the Free Software 017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018//////////////////////////////////////////////////////////////////////////////// 019 020package com.puppycrawl.tools.checkstyle.checks; 021 022import java.nio.charset.StandardCharsets; 023import java.util.Arrays; 024 025/** 026 * Represents the options for line separator settings. 027 * 028 * @author lkuehne 029 * @see NewlineAtEndOfFileCheck 030 */ 031public enum LineSeparatorOption { 032 /** Windows-style line separators. **/ 033 CRLF("\r\n"), 034 035 /** Mac-style line separators. **/ 036 CR("\r"), 037 038 /** Unix-style line separators. **/ 039 LF("\n"), 040 041 /** 042 * Matches CR, LF and CRLF line separators. 043 * Only the length is used - the actual value is ignored. 044 */ 045 LF_CR_CRLF("##"), 046 047 /** System default line separators. **/ 048 SYSTEM(System.getProperty("line.separator")); 049 050 /** The line separator representation. */ 051 private final byte[] lineSeparator; 052 053 /** 054 * Creates a new {@code LineSeparatorOption} instance. 055 * @param sep the line separator, e.g. "\r\n" 056 */ 057 LineSeparatorOption(String sep) { 058 lineSeparator = sep.getBytes(StandardCharsets.US_ASCII); 059 } 060 061 /** 062 * Checks that bytes is equal to the byte representation of this line separator. 063 * @param bytes a bytes array to check 064 * @return if bytes is equal to the byte representation 065 * of this line separator 066 */ 067 public boolean matches(byte... bytes) { 068 final boolean result; 069 if (this == LF_CR_CRLF) { 070 // this silently assumes CRLF and ANY have the same length 071 // and LF and CR are of length 1 072 result = CRLF.matches(bytes) 073 || LF.matches(Arrays.copyOfRange(bytes, 1, 2)) 074 || CR.matches(Arrays.copyOfRange(bytes, 1, 2)); 075 } 076 else { 077 result = Arrays.equals(bytes, lineSeparator); 078 } 079 return result; 080 } 081 082 /** 083 * Returns length of file separator in bytes. 084 * @return the length of the file separator in bytes, 085 * e.g. 1 for CR, 2 for CRLF, ... 086 */ 087 public int length() { 088 return lineSeparator.length; 089 } 090}