1 2 24 25 26 27 28 package com.lutris.util; 29 30 33 public final class QuotedString { 34 47 public static final 48 String parseCString(String s) 49 { 50 return parseCString(s.toCharArray(), 0); 51 } 52 53 68 public static final 69 String parseCString(String s, int offset) 70 { 71 return parseCString(s.toCharArray(), offset); 72 } 73 74 87 public static final 88 String parseCString(char[] c) 89 { 90 return parseCString(c, 0); 91 } 92 93 108 public static final 109 String parseCString(char[] c, int offset) 110 { 111 int pos, end, len; 112 len = c.length; 113 pos = offset; 114 115 if (pos > len) return null; 116 if (pos == len) return ""; 117 118 if (c[pos] == '"') { 119 StringBuffer b = new StringBuffer (); 120 while (++pos < len) { 121 switch (c[pos]) { 122 case '"': 123 return new String (b); 124 case '\\': 125 if (++pos >= len) { 126 b.append('\\'); 127 return new String (b); 128 } 129 if (c[pos] == '0') { 130 if ((len - pos) >= 3) { 131 String xs = new String (c, pos, 3); 132 int i=-1;; 133 try { i = Integer.parseInt(xs, 8); } 134 catch (Throwable t) { i = -1; } 135 if ((i >= 0) && (i <= 255)) { 136 b.append((char)i); 137 pos += 2; 138 } else { 139 b.append(c[pos]); 140 } 141 } else { 142 b.append(c[pos]); 143 } 144 } else { 145 b.append(c[pos]); 146 } 147 break; 148 default: 149 b.append(c[pos]); 150 break; 151 } 152 } 153 return new String (b); 154 } else { 155 end = pos; 156 while ((end < len) && (c[end] > ' ')) end++; 157 if ((end - pos) <= 0) return ""; 158 return new String (c, pos, end - pos); 159 } 160 } 161 } 162 | Popular Tags |