15267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)/*
25267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
35267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) *
45267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * modification, are permitted provided that the following conditions are
65267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * met:
75267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) *
85267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * in the documentation and/or other materials provided with the
135267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * distribution.
145267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * contributors may be used to endorse or promote products derived from
165267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * this software without specific prior written permission.
175267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) *
185267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) */
305267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
31a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "config.h"
32a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "wtf/testing/WTFTestHelpers.h"
335267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
34a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "wtf/text/WTFString.h"
35a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include <ios> // NOLINT
36a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include <ostream> // NOLINT
375267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
38a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)namespace WTF {
395267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
40a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)std::ostream& operator<<(std::ostream& out, const String& string)
41a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
42a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    if (string.isNull())
43a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        return out << "<null>";
445267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
45a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    out << '"';
46a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    for (unsigned index = 0; index < string.length(); ++index) {
47a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        // Print shorthands for select cases.
48a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        UChar character = string[index];
49a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        switch (character) {
50a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        case '\t':
51a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            out << "\\t";
52a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            break;
53a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        case '\n':
54a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            out << "\\n";
55a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            break;
56a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        case '\r':
57a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            out << "\\r";
58a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            break;
59a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        case '"':
60a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            out << "\\\"";
61a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            break;
62a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        case '\\':
63a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            out << "\\\\";
64a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            break;
65a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        default:
66a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            if (character >= 0x20 && character < 0x7F) {
67a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)                out << static_cast<char>(character);
68a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            } else {
69a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)                // Print "\uXXXX" for control or non-ASCII characters.
70a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)                out << "\\u";
71a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)                out.width(4);
72a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)                out.fill('0');
73a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)                out.setf(std::ios_base::hex, std::ios_base::basefield);
74a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)                out.setf(std::ios::uppercase);
75a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)                out << character;
76a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            }
77a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)            break;
78a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        }
79a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    }
80a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    return out << '"';
81a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
825267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
83a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)} // namespace WTF
84