Home | History | Annotate | Download | only in text
      1 /*
      2  * Copyright (C) 2004, 2008, 2010 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "core/platform/text/TextStream.h"
     28 
     29 #include "wtf/MathExtras.h"
     30 #include "wtf/StringExtras.h"
     31 #include "wtf/text/WTFString.h"
     32 
     33 using namespace std;
     34 
     35 namespace WebCore {
     36 
     37 static const size_t printBufferSize = 100; // large enough for any integer or floating point value in string format, including trailing null character
     38 
     39 static inline bool hasFractions(double val)
     40 {
     41     static const double s_epsilon = 0.0001;
     42     int ival = static_cast<int>(val);
     43     double dval = static_cast<double>(ival);
     44     return fabs(val - dval) > s_epsilon;
     45 }
     46 
     47 TextStream& TextStream::operator<<(bool b)
     48 {
     49     return *this << (b ? "1" : "0");
     50 }
     51 
     52 TextStream& TextStream::operator<<(int i)
     53 {
     54     m_text.appendNumber(i);
     55     return *this;
     56 }
     57 
     58 TextStream& TextStream::operator<<(unsigned i)
     59 {
     60     m_text.appendNumber(i);
     61     return *this;
     62 }
     63 
     64 TextStream& TextStream::operator<<(long i)
     65 {
     66     m_text.appendNumber(i);
     67     return *this;
     68 }
     69 
     70 TextStream& TextStream::operator<<(unsigned long i)
     71 {
     72     m_text.appendNumber(i);
     73     return *this;
     74 }
     75 
     76 TextStream& TextStream::operator<<(long long i)
     77 {
     78     m_text.appendNumber(i);
     79     return *this;
     80 }
     81 
     82 TextStream& TextStream::operator<<(unsigned long long i)
     83 {
     84     m_text.appendNumber(i);
     85     return *this;
     86 }
     87 
     88 TextStream& TextStream::operator<<(float f)
     89 {
     90     m_text.append(String::numberToStringFixedWidth(f, 2));
     91     return *this;
     92 }
     93 
     94 TextStream& TextStream::operator<<(double d)
     95 {
     96     m_text.append(String::numberToStringFixedWidth(d, 2));
     97     return *this;
     98 }
     99 
    100 TextStream& TextStream::operator<<(const char* string)
    101 {
    102     m_text.append(string);
    103     return *this;
    104 }
    105 
    106 TextStream& TextStream::operator<<(const void* p)
    107 {
    108     char buffer[printBufferSize];
    109     snprintf(buffer, sizeof(buffer) - 1, "%p", p);
    110     return *this << buffer;
    111 }
    112 
    113 TextStream& TextStream::operator<<(const String& string)
    114 {
    115     m_text.append(string);
    116     return *this;
    117 }
    118 
    119 TextStream& TextStream::operator<<(const FormatNumberRespectingIntegers& numberToFormat)
    120 {
    121     if (hasFractions(numberToFormat.value))
    122         return *this << numberToFormat.value;
    123 
    124     m_text.appendNumber(static_cast<int>(numberToFormat.value));
    125     return *this;
    126 }
    127 
    128 String TextStream::release()
    129 {
    130     String result = m_text.toString();
    131     m_text.clear();
    132     return result;
    133 }
    134 
    135 }
    136