Esempio n. 1
0
FTSelection *FTDistance::optimize(FTContext *ftcontext, bool execute) const
{
    XPath2MemoryManager *mm = ftcontext->context->getMemoryManager();

    if(execute || range_.arg1->isConstant()) {
        Result rangeResult = range_.arg1->createResult(ftcontext->context);
        Numeric::Ptr num = (Numeric::Ptr)rangeResult->next(ftcontext->context);
        long distance = ::atol(UTF8(num->asString(ftcontext->context)));

        switch(range_.type) {
        case FTRange::EXACTLY: {
            FTSelection *result = new (mm) FTDistanceLiteral(arg_, FTRange::EXACTLY, distance, 0, unit_, mm);
            result->setLocationInfo(this);
            return result->optimize(ftcontext, execute);
        }
        case FTRange::AT_LEAST: {
            FTSelection *result = new (mm) FTDistanceLiteral(arg_, FTRange::AT_LEAST, distance, 0, unit_, mm);
            result->setLocationInfo(this);
            return result->optimize(ftcontext, execute);
        }
        case FTRange::AT_MOST: {
            FTSelection *result = new (mm) FTDistanceLiteral(arg_, FTRange::AT_MOST, distance, 0, unit_, mm);
            result->setLocationInfo(this);
            return result->optimize(ftcontext, execute);
        }
        case FTRange::FROM_TO: {
            Result rangeResult2 = range_.arg2->createResult(ftcontext->context);
            Numeric::Ptr num2 = (Numeric::Ptr)rangeResult2->next(ftcontext->context);
            long distance2 = ::atol(UTF8(num->asString(ftcontext->context)));

            FTSelection *result = new (mm) FTDistanceLiteral(arg_, FTRange::FROM_TO, distance, distance2, unit_, mm);
            result->setLocationInfo(this);
            return result->optimize(ftcontext, execute);
        }
        }
    }

    FTSelection *newarg = arg_->optimize(ftcontext, execute);
    if(newarg == 0) return 0;

    if(newarg->getType() == WORD) {
        return newarg;
    }

    newarg = new (mm) FTDistance(range_, unit_, newarg, mm);
    newarg->setLocationInfo(this);
    return newarg;
}
Esempio n. 2
0
Sequence FunctionRegexGroup::createSequence(DynamicContext *context, int flags) const
{
    XPath2MemoryManager *mm = context->getMemoryManager();

    const RegexGroupStore *store = context->getRegexGroupStore();
    if(store == 0)
        return Sequence(context->getItemFactory()->createString(XMLUni::fgZeroLenString, context), mm);

    Numeric::Ptr indexItem = getParamNumber(1, context)->next(context);
    if(indexItem->isNegative())
        return Sequence(context->getItemFactory()->createString(XMLUni::fgZeroLenString, context), mm);

    const XMLCh *indexStr = indexItem->asString(context);

    int index = 0;
    while(*indexStr != 0) {
        if(*indexStr >= '0' && *indexStr <= '9') {
            index *= 10;
            index += *indexStr - '0';
        }
        ++indexStr;
    }

    const XMLCh *result = store->getGroup(index);
    if(result == 0) result = XMLUni::fgZeroLenString;

    return Sequence(context->getItemFactory()->createString(result, context), mm);
}