| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 13 |
import net.sf.saxon.om.One; |
| 14 |
import net.sf.saxon.om.Sequence; |
| 15 |
import net.sf.saxon.om.ZeroOrOne; |
| 16 |
import net.sf.saxon.regex.EmptyString; |
| 17 |
import net.sf.saxon.regex.UnicodeString; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 20 |
import net.sf.saxon.type.TypeHierarchy; |
| 21 |
import net.sf.saxon.value.Int64Value; |
| 22 |
import net.sf.saxon.value.NumericValue; |
| 23 |
import net.sf.saxon.value.StringValue; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class Substring extends SystemFunction implements Callable { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public Expression typeCheckCaller(FunctionCall caller, ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 39 |
Expression e2 = super.typeCheckCaller(caller, visitor, contextInfo); |
| 40 |
if (e2 != caller) { |
| 41 |
return e2; |
| 42 |
} |
| 43 |
TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); |
| 44 |
if (caller.getArg(1).isCallOn(Number_1.class)) { |
| 45 |
Expression a1 = ((StaticFunctionCall)caller.getArg(1)).getArg(0); |
| 46 |
if (th.isSubType(a1.getItemType(), BuiltInAtomicType.INTEGER)) { |
| 47 |
caller.setArg(1, a1); |
| 48 |
} |
| 49 |
} |
| 50 |
if (getArity() > 2 && caller.getArg(2).isCallOn(Number_1.class)) { |
| 51 |
Expression a2 = ((StaticFunctionCall) caller.getArg(2)).getArg(0); |
| 52 |
if (th.isSubType(a2.getItemType(), BuiltInAtomicType.INTEGER)) { |
| 53 |
caller.setArg(2, a2); |
| 54 |
} |
| 55 |
} |
| 56 |
return caller; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static UnicodeString substring(StringValue sv, NumericValue start) { |
| 69 |
UnicodeString s = sv.getUnicodeString(); |
| 70 |
int slength = s.uLength(); |
| 71 |
|
| 72 |
long lstart; |
| 73 |
if (start instanceof Int64Value) { |
| 74 |
|
| 75 |
lstart = ((Int64Value) start).longValue(); |
| 76 |
if (lstart > slength) { |
| 77 |
return EmptyString.THE_INSTANCE; |
| 78 |
} else if (lstart <= 0) { |
| 79 |
lstart = 1; |
| 80 |
} |
| 81 |
} else { |
| 82 |
|
| 83 |
|
| 84 |
if (start.isNaN()) { |
| 85 |
return EmptyString.THE_INSTANCE; |
| 86 |
} else if (start.signum() <= 0) { |
| 87 |
return s; |
| 88 |
} else if (start.compareTo(slength) > 0) { |
| 89 |
|
| 90 |
|
| 91 |
return EmptyString.THE_INSTANCE; |
| 92 |
} else { |
| 93 |
lstart = Math.round(start.getDoubleValue()); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if (lstart > s.uLength()) { |
| 98 |
return EmptyString.THE_INSTANCE; |
| 99 |
} |
| 100 |
return s.uSubstring((int) lstart - 1, s.uLength()); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public static UnicodeString substring(StringValue sv, NumericValue start, NumericValue len) { |
| 115 |
|
| 116 |
int slength = sv.getStringLengthUpperBound(); |
| 117 |
|
| 118 |
long lstart; |
| 119 |
if (start instanceof Int64Value) { |
| 120 |
|
| 121 |
lstart = ((Int64Value) start).longValue(); |
| 122 |
if (lstart > slength) { |
| 123 |
return EmptyString.THE_INSTANCE; |
| 124 |
} |
| 125 |
} else { |
| 126 |
|
| 127 |
if (start.isNaN()) { |
| 128 |
return EmptyString.THE_INSTANCE; |
| 129 |
} else if (start.compareTo(slength) > 0) { |
| 130 |
|
| 131 |
|
| 132 |
return EmptyString.THE_INSTANCE; |
| 133 |
} else { |
| 134 |
double dstart = start.getDoubleValue(); |
| 135 |
lstart = Double.isInfinite(dstart) ? -Integer.MAX_VALUE : Math.round(dstart); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
long llen; |
| 140 |
if (len instanceof Int64Value) { |
| 141 |
llen = ((Int64Value) len).longValue(); |
| 142 |
if (llen <= 0) { |
| 143 |
return EmptyString.THE_INSTANCE; |
| 144 |
} |
| 145 |
} else { |
| 146 |
if (len.isNaN()) { |
| 147 |
return EmptyString.THE_INSTANCE; |
| 148 |
} |
| 149 |
if (len.signum() <= 0) { |
| 150 |
return EmptyString.THE_INSTANCE; |
| 151 |
} |
| 152 |
double dlen = len.getDoubleValue(); |
| 153 |
if (Double.isInfinite(dlen)) { |
| 154 |
llen = Integer.MAX_VALUE; |
| 155 |
} else { |
| 156 |
llen = Math.round(len.getDoubleValue()); |
| 157 |
} |
| 158 |
} |
| 159 |
long lend = lstart + llen; |
| 160 |
if (lend < lstart) { |
| 161 |
return EmptyString.THE_INSTANCE; |
| 162 |
} |
| 163 |
|
| 164 |
UnicodeString us = sv.getUnicodeString(); |
| 165 |
int clength = us.uLength(); |
| 166 |
int a1 = (int) lstart - 1; |
| 167 |
if (a1 >= clength) { |
| 168 |
return EmptyString.THE_INSTANCE; |
| 169 |
} |
| 170 |
int a2 = Math.min(clength, (int) lend - 1); |
| 171 |
if (a1 < 0) { |
| 172 |
if (a2 < 0) { |
| 173 |
return EmptyString.THE_INSTANCE; |
| 174 |
} else { |
| 175 |
a1 = 0; |
| 176 |
} |
| 177 |
} |
| 178 |
return us.uSubstring(a1, a2); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public ZeroOrOne<StringValue> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 191 |
StringValue arg0 = (StringValue) arguments[0].head(); |
| 192 |
if (arg0 == null) { |
| 193 |
return One.string(""); |
| 194 |
} |
| 195 |
NumericValue arg1 = (NumericValue) arguments[1].head(); |
| 196 |
if (arguments.length == 2) { |
| 197 |
return new ZeroOrOne<>(StringValue.makeStringValue(substring(arg0, arg1))); |
| 198 |
} else { |
| 199 |
NumericValue arg2 = (NumericValue) arguments[2].head(); |
| 200 |
return new ZeroOrOne<>(StringValue.makeStringValue(substring(arg0, arg1, arg2))); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
public String getCompilerName() { |
| 205 |
return "SubstringCompiler"; |
| 206 |
} |
| 207 |
} |
| 208 |
|