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 **/ 019package lucee.runtime.functions.string; 020 021import lucee.commons.lang.StringUtil; 022import lucee.runtime.PageContext; 023import lucee.runtime.exp.ExpressionException; 024import lucee.runtime.exp.PageException; 025import lucee.runtime.op.Caster; 026 027public class ParseNumber { 028 private static final int BIN=2; 029 private static final int OCT=8; 030 private static final int DEC=10; 031 private static final int HEX=16; 032 033 public static double call(PageContext pc , String strNumber) throws PageException { 034 return call(pc,strNumber,null); 035 } 036 037 public static double call(PageContext pc , String strNumber,String strRadix) throws PageException { 038 return invoke(strNumber, strRadix); 039 } 040 041 public static double invoke(String strNumber,String strRadix, double defaultValue) { 042 try { 043 return invoke(strNumber, strRadix); 044 } catch (PageException e) { 045 return defaultValue; 046 } 047 } 048 public static double invoke(String strNumber,String strRadix) throws PageException { 049 strNumber=strNumber.trim(); 050 int radix=DEC; 051 if(strRadix==null) { 052 if(StringUtil.startsWithIgnoreCase(strNumber, "0x")){ 053 radix=HEX; 054 strNumber=strNumber.substring(2); 055 } 056 else if(strNumber.startsWith("#")){ 057 radix=HEX; 058 strNumber=strNumber.substring(1); 059 } 060 else if(strNumber.startsWith("0") && strNumber.length()>1){ 061 radix=OCT; 062 strNumber=strNumber.substring(1); 063 } 064 } 065 else { 066 strRadix=strRadix.trim().toLowerCase(); 067 068 if(strRadix.startsWith("bin"))radix=BIN; 069 else if(strRadix.startsWith("oct"))radix=OCT; 070 else if(strRadix.startsWith("dec"))radix=DEC; 071 else if(strRadix.startsWith("hex")){ 072 if(StringUtil.startsWithIgnoreCase(strNumber, "0x")) strNumber=strNumber.substring(2); 073 else if(strNumber.startsWith("#")) strNumber=strNumber.substring(1); 074 075 radix=HEX; 076 } 077 else throw new ExpressionException("invalid radix defintions, valid vales are [bin,oct,dec,hex]"); 078 079 } 080 081 if(radix==OCT && strNumber.indexOf('9')!=-1) 082 throw new ExpressionException("digit [9] is out of range for a octal number"); 083 084 085 if(strNumber.indexOf('.')!=-1 && radix!=DEC) 086 throw new ExpressionException("the radix con only be [dec] for floating point numbers"); 087 088 089 if(radix==DEC) { 090 return Caster.toDoubleValue(strNumber); 091 } 092 return Integer.parseInt(strNumber,radix); 093 } 094 095}