001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 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, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019 020package lucee.runtime.op.date; 021 022import java.util.HashMap; 023import java.util.Map; 024 025import lucee.runtime.op.Constants; 026 027 028/** 029 * helper class to convert a string to a Object 030 */ 031public final class DateString { 032 033 private static int[][] ints= { 034 {0,1,2,3,4,5,6,7,8,9}, 035 {0,10,20,30,40,50,60,70,80,90}, 036 {0,100,200,300,400,500,600,700,800,900}, 037 {0,1000,2000,3000,4000,5000,6000,7000,8000,9000} 038 }; 039 040 private String str; 041 private int pos; 042 043 private static Map months=new HashMap(); 044 static{ 045 months.put("january",Constants.INTEGER_1); 046 months.put("januar",Constants.INTEGER_1); 047 months.put("janua",Constants.INTEGER_1); 048 months.put("janu",Constants.INTEGER_1); 049 months.put("jan",Constants.INTEGER_1); 050 051 months.put("february",Constants.INTEGER_2); 052 months.put("februar",Constants.INTEGER_2); 053 months.put("februa",Constants.INTEGER_2); 054 months.put("febru",Constants.INTEGER_2); 055 months.put("febr",Constants.INTEGER_2); 056 months.put("feb",Constants.INTEGER_2); 057 058 months.put("march",Constants.INTEGER_3); 059 months.put("marc",Constants.INTEGER_3); 060 months.put("mar",Constants.INTEGER_3); 061 062 months.put("april",Constants.INTEGER_4); 063 months.put("apri",Constants.INTEGER_4); 064 months.put("apr",Constants.INTEGER_4); 065 066 months.put("may",Constants.INTEGER_5); 067 068 months.put("june",Constants.INTEGER_6); 069 months.put("jun",Constants.INTEGER_6); 070 071 months.put("july",Constants.INTEGER_7); 072 months.put("jul",Constants.INTEGER_7); 073 074 months.put("august",Constants.INTEGER_8); 075 months.put("augus",Constants.INTEGER_8); 076 months.put("augu",Constants.INTEGER_8); 077 months.put("aug",Constants.INTEGER_8); 078 079 months.put("september",Constants.INTEGER_9); 080 months.put("septembe",Constants.INTEGER_9); 081 months.put("septemb",Constants.INTEGER_9); 082 months.put("septem",Constants.INTEGER_9); 083 months.put("septe",Constants.INTEGER_9); 084 months.put("sept",Constants.INTEGER_9); 085 months.put("sep",Constants.INTEGER_9); 086 087 months.put("october",Constants.INTEGER_10); 088 months.put("octobe",Constants.INTEGER_10); 089 months.put("octob",Constants.INTEGER_10); 090 months.put("octo",Constants.INTEGER_10); 091 months.put("oct",Constants.INTEGER_10); 092 093 months.put("november",Constants.INTEGER_11); 094 months.put("novembe",Constants.INTEGER_11); 095 months.put("novemb",Constants.INTEGER_11); 096 months.put("novem",Constants.INTEGER_11); 097 months.put("nove",Constants.INTEGER_11); 098 months.put("nov",Constants.INTEGER_11); 099 100 months.put("december",Constants.INTEGER_12); 101 months.put("decembe",Constants.INTEGER_12); 102 months.put("decemb",Constants.INTEGER_12); 103 months.put("decem",Constants.INTEGER_12); 104 months.put("dece",Constants.INTEGER_12); 105 months.put("dec",Constants.INTEGER_12); 106 107 108 } 109 110 /** 111 * constructor of the class 112 * @param str Date String 113 */ 114 public DateString(String str) { 115 this.str=str; 116 } 117 118 /** 119 * check if char a actuell position of the inner cursor is same value like given value 120 * @param c char to compare 121 * @return is same or not 122 */ 123 public boolean isNext(char c) { 124 return str.length()>pos+1 && str.charAt(pos+1)==c; 125 } 126 127 /** 128 * check if char a actuell position of the inner cursor is same value like given value 129 * @param c char to compare 130 * @return is same or not 131 */ 132 public boolean isCurrent(char c) { 133 return str.length()>pos && str.charAt(pos)==c; 134 } 135 136 /** 137 * check if last char has same value than given char 138 * @param c char to check 139 * @return is same or not 140 */ 141 public boolean isLast(char c) { 142 return str.charAt(str.length()-1)==c; 143 } 144 145 /** 146 * set inner cursor one forward 147 */ 148 public void next() { 149 pos++; 150 } 151 152 /** 153 * set inner cursor [count] forward 154 * @param count forward count 155 */ 156 public void next(int count) { 157 pos+=count; 158 } 159 160 /** 161 * @return the length of the inner String 162 */ 163 public int length() { 164 return str.length(); 165 } 166 167 /** 168 * forward inner cursor if value at actuell position is same as given. 169 * @param c char to compare 170 * @return has forwared or not 171 */ 172 public boolean fwIfCurrent(char c) { 173 if(isCurrent(c)) { 174 pos++; 175 return true; 176 } 177 return false; 178 } 179 180 /** 181 * forward inner cursor if value at the next position is same as given. 182 * @param c char to compare 183 * @return has forwared or not 184 */ 185 public boolean fwIfNext(char c) { 186 if(isNext(c)) { 187 pos++; 188 return true; 189 } 190 return false; 191 } 192 193 /* 194 * read in the next four digits from current position 195 * @return value from the 4 digits 196 * 197 public int read4Digit() { 198 // first 199 char c=str.charAt(pos++); 200 if(!isDigit(c)) return -1; 201 int value=ints[3][c-48]; 202 203 // second 204 c=str.charAt(pos++); 205 if(!isDigit(c)) return -1; 206 value+=ints[2][c-48]; 207 208 // third 209 c=str.charAt(pos++); 210 if(!isDigit(c)) return -1; 211 value+=ints[1][c-48]; 212 213 // fourt 214 c=str.charAt(pos++); 215 if(!isDigit(c)) return -1; 216 value+=ints[0][c-48]; 217 218 return value; 219 }*/ 220 221 /* 222 * read in the next four digits from current position 223 * @return value from the 4 digits 224 * 225 public int read2Digit() { 226 // first 227 char c=str.charAt(pos++); 228 if(!isDigit(c)) return -1; 229 int value=ints[1][c-48]; 230 231 // second 232 c=str.charAt(pos++); 233 if(!isDigit(c)) return -1; 234 value+=ints[0][c-48]; 235 236 return value; 237 }*/ 238 239 /** 240 * read in the next digits from current position 241 * @return value from the digits 242 */ 243 public int readDigits() { 244 int value=0; 245 char c; 246 if(isValidIndex() && isDigit(c=str.charAt(pos))) { 247 value=ints[0][c-48]; 248 pos++; 249 } 250 else return -1; 251 while(isValidIndex() && isDigit(c=str.charAt(pos))) { 252 value*=10; 253 value+=ints[0][c-48]; 254 pos++; 255 } 256 return value; 257 } 258 public boolean removeWhitespace() { 259 boolean rtn=false; 260 while(isValidIndex() && Character.isWhitespace(str.charAt(pos))) { 261 pos++; 262 } 263 return rtn; 264 } 265 266 267 268 public int readMonthString() { 269 char c; 270 int start=pos; 271 StringBuilder sb=new StringBuilder(); 272 while(isValidIndex() && isMonthChar(c=str.charAt(pos))) { 273 pos++; 274 sb.append(Character.toLowerCase(c)); 275 } 276 Integer month=(Integer) months.get(sb.toString().trim()); 277 if(month!=null) return month.intValue(); 278 pos=start; 279 return -1; 280 } 281 282 283 private boolean isMonthChar(char c) { 284 return c>='a' && c<='z' || c>='A' && c<='Z'; 285 } 286 287 /** 288 * returns if c is a digit or not 289 * @param c char to check 290 * @return is digit 291 */ 292 public boolean isDigit(char c) { 293 return c>='0' && c<='9'; 294 } 295 296 297 /** 298 * returns if value at cursor position is a digit or not 299 * @return is dgit 300 */ 301 public boolean isDigit() { 302 return isValidIndex() && isDigit(str.charAt(pos)); 303 } 304 305 /** 306 * returns if last char is a digit or not 307 * @return is dgit 308 */ 309 public boolean isLastDigit() { 310 return isDigit(str.charAt(str.length()-1)); 311 } 312 313 /** 314 * return char at given position 315 * @param pos postion to get value 316 * @return character from given position 317 */ 318 public char charAt(int pos) { 319 return str.charAt(pos); 320 } 321 322 /** 323 * returns if cursor is on the last position 324 * @return is on last 325 */ 326 public boolean isLast() { 327 return pos+1==str.length(); 328 } 329 330 /** 331 * returns if cursor is after the last position 332 * @return is after the last 333 */ 334 public boolean isAfterLast() { 335 return pos>=str.length(); 336 } 337 338 /** 339 * returns if cursor is on a valid position 340 * @return is after the last 341 */ 342 public boolean isValidIndex() { 343 return pos<str.length(); 344 } 345 346 public char current() { 347 return str.charAt(pos); 348 } 349 public int getPos() { 350 return pos; 351 } 352 353 public void reset(){ 354 pos=0; 355 } 356 357}