1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 module hunt.net.codec.textline.LineDelimiter; 21 22 import hunt.Exceptions; 23 import hunt.util.StringBuilder; 24 25 26 import std.array; 27 import std.ascii; 28 import std.format; 29 import std.string; 30 // import java.io.ByteArrayOutputStream; 31 // import java.io.PrintWriter; 32 33 /** 34 * A delimiter which is appended to the end of a text line, such as 35 * <tt>CR/LF</tt>. This class defines default delimiters for various 36 * OS : 37 * <ul> 38 * <li><b>Unix/Linux</b> : LineDelimiter.UNIX ("\n")</li> 39 * <li><b>Windows</b> : LineDelimiter.WINDOWS ("\r\n")</li> 40 * <li><b>MAC</b> : LineDelimiter.MAC ("\r")</li> 41 * </ul> 42 * 43 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 44 */ 45 struct LineDelimiter { 46 /** the line delimiter constant of the current O/S. */ 47 enum LineDelimiter DEFAULT = LineDelimiter(newline); 48 49 /** 50 * A special line delimiter which is used for auto-detection of 51 * EOL in {@link TextLineDecoder}. If this delimiter is used, 52 * {@link TextLineDecoder} will consider both <tt>'\r'</tt> and 53 * <tt>'\n'</tt> as a delimiter. 54 */ 55 enum LineDelimiter AUTO = LineDelimiter(""); 56 57 /** 58 * The CRLF line delimiter constant (<tt>"\r\n"</tt>) 59 */ 60 enum LineDelimiter CRLF = LineDelimiter("\r\n"); 61 62 /** 63 * The line delimiter constant of UNIX (<tt>"\n"</tt>) 64 */ 65 enum LineDelimiter UNIX = LineDelimiter("\n"); 66 67 /** 68 * The line delimiter constant of MS Windows/DOS (<tt>"\r\n"</tt>) 69 */ 70 enum LineDelimiter WINDOWS = CRLF; 71 72 /** 73 * The line delimiter constant of Mac OS (<tt>"\r"</tt>) 74 */ 75 enum LineDelimiter MAC = LineDelimiter("\r"); 76 77 /** 78 * The line delimiter constant for NUL-terminated text protocols 79 * such as Flash XML socket (<tt>"\0"</tt>) 80 */ 81 enum LineDelimiter NUL = LineDelimiter("\0"); 82 83 /** Stores the selected Line delimiter */ 84 private string value; 85 86 /** 87 * Creates a new line delimiter with the specified <tt>value</tt>. 88 * 89 * @param value The new Line Delimiter 90 */ 91 this(string value) { 92 if(!__ctfe) { 93 if (value.empty) { 94 throw new IllegalArgumentException("delimiter"); 95 } 96 } 97 98 this.value = value; 99 } 100 101 /** 102 * @return the delimiter string. 103 */ 104 string getValue() { 105 return value; 106 } 107 108 /** 109 * {@inheritDoc} 110 */ 111 size_t toHash() @trusted nothrow { 112 return hashOf(value); 113 } 114 115 /** 116 * {@inheritDoc} 117 */ 118 bool opEquals(ref LineDelimiter o) { 119 return this.value == o.value; 120 } 121 122 /** 123 * {@inheritDoc} 124 */ 125 string toString() { 126 if (value.length == 0) { 127 return "delimiter: auto"; 128 } else { 129 StringBuilder buf = new StringBuilder(); 130 buf.append("delimiter:"); 131 132 for (int i = 0; i < value.length; i++) { 133 buf.append(" 0x"); 134 buf.append(format("%02X", value[i])); 135 } 136 137 return buf.toString(); 138 } 139 } 140 }