1/*
2 * Copyright 2005 Frerich Raabe <raabe@kde.org>
3 * Copyright (C) 2006 Apple Computer, Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "XPathValue.h"
29
30#include "Node.h"
31#include "XPathExpressionNode.h"
32#include "XPathUtil.h"
33#include <limits>
34#include <wtf/MathExtras.h>
35#include <wtf/StdLibExtras.h>
36
37using std::numeric_limits;
38
39namespace WebCore {
40namespace XPath {
41
42const Value::AdoptTag Value::adopt = {};
43
44const NodeSet& Value::toNodeSet() const
45{
46    if (!isNodeSet())
47        Expression::evaluationContext().hadTypeConversionError = true;
48
49    if (!m_data) {
50        DEFINE_STATIC_LOCAL(NodeSet, emptyNodeSet, ());
51        return emptyNodeSet;
52    }
53
54    return m_data->m_nodeSet;
55}
56
57NodeSet& Value::modifiableNodeSet()
58{
59    if (!isNodeSet())
60        Expression::evaluationContext().hadTypeConversionError = true;
61
62    if (!m_data)
63        m_data = ValueData::create();
64
65    m_type = NodeSetValue;
66    return m_data->m_nodeSet;
67}
68
69bool Value::toBoolean() const
70{
71    switch (m_type) {
72        case NodeSetValue:
73            return !m_data->m_nodeSet.isEmpty();
74        case BooleanValue:
75            return m_bool;
76        case NumberValue:
77            return m_number && !std::isnan(m_number);
78        case StringValue:
79            return !m_data->m_string.isEmpty();
80    }
81    ASSERT_NOT_REACHED();
82    return false;
83}
84
85double Value::toNumber() const
86{
87    switch (m_type) {
88        case NodeSetValue:
89            return Value(toString()).toNumber();
90        case NumberValue:
91            return m_number;
92        case StringValue: {
93            const String& str = m_data->m_string.simplifyWhiteSpace();
94
95            // String::toDouble() supports exponential notation, which is not allowed in XPath.
96            unsigned len = str.length();
97            for (unsigned i = 0; i < len; ++i) {
98                UChar c = str[i];
99                if (!isASCIIDigit(c) && c != '.'  && c != '-')
100                    return numeric_limits<double>::quiet_NaN();
101            }
102
103            bool canConvert;
104            double value = str.toDouble(&canConvert);
105            if (canConvert)
106                return value;
107            return numeric_limits<double>::quiet_NaN();
108        }
109        case BooleanValue:
110            return m_bool;
111    }
112    ASSERT_NOT_REACHED();
113    return 0.0;
114}
115
116String Value::toString() const
117{
118    switch (m_type) {
119        case NodeSetValue:
120            if (m_data->m_nodeSet.isEmpty())
121                return "";
122            return stringValue(m_data->m_nodeSet.firstNode());
123        case StringValue:
124            return m_data->m_string;
125        case NumberValue:
126            if (std::isnan(m_number))
127                return "NaN";
128            if (m_number == 0)
129                return "0";
130            if (std::isinf(m_number))
131                return std::signbit(m_number) ? "-Infinity" : "Infinity";
132            return String::number(m_number);
133        case BooleanValue:
134            return m_bool ? "true" : "false";
135    }
136    ASSERT_NOT_REACHED();
137    return String();
138}
139
140}
141}
142