Navigation Menu

Skip to content

Commit

Permalink
Fix wrong guard macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yata committed Jun 28, 2013
1 parent 9caf2b5 commit 87208b8
Show file tree
Hide file tree
Showing 11 changed files with 509 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/grnxx/broken_down_time.cpp
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013 Brazil, Inc.
Copyright (C) 2012-2013 Brazil, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down
8 changes: 4 additions & 4 deletions lib/grnxx/broken_down_time.hpp
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013 Brazil, Inc.
Copyright (C) 2012-2013 Brazil, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -15,8 +15,8 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef GRNXX_TIME_BROKEN_DOWN_TIME_HPP
#define GRNXX_TIME_BROKEN_DOWN_TIME_HPP
#ifndef GRNXX_BROKEN_DOWN_TIME_HPP
#define GRNXX_BROKEN_DOWN_TIME_HPP

#include "grnxx/features.hpp"

Expand Down Expand Up @@ -45,4 +45,4 @@ StringBuilder &operator<<(StringBuilder &builder, const BrokenDownTime &time);

} // namespace grnxx

#endif // GRNXX_TIME_BROKEN_DOWN_TIME_HPP
#endif // GRNXX_BROKEN_DOWN_TIME_HPP
6 changes: 3 additions & 3 deletions lib/grnxx/duration.hpp
Expand Up @@ -15,8 +15,8 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef GRNXX_TIME_DURATION_HPP
#define GRNXX_TIME_DURATION_HPP
#ifndef GRNXX_DURATION_HPP
#define GRNXX_DURATION_HPP

#include "grnxx/features.hpp"

Expand Down Expand Up @@ -169,4 +169,4 @@ StringBuilder &operator<<(StringBuilder &builder, Duration duration);

} // namespace grnxx

#endif // GRNXX_TIME_DURATION_HPP
#endif // GRNXX_DURATION_HPP
2 changes: 1 addition & 1 deletion lib/grnxx/periodic_clock.cpp
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013 Brazil, Inc.
Copyright (C) 2012-2013 Brazil, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand Down
8 changes: 4 additions & 4 deletions lib/grnxx/periodic_clock.hpp
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013 Brazil, Inc.
Copyright (C) 2012-2013 Brazil, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
Expand All @@ -15,8 +15,8 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef GRNXX_TIME_PERIODIC_CLOCK_HPP
#define GRNXX_TIME_PERIODIC_CLOCK_HPP
#ifndef GRNXX_PERIODIC_CLOCK_HPP
#define GRNXX_PERIODIC_CLOCK_HPP

#include "grnxx/features.hpp"

Expand All @@ -42,4 +42,4 @@ class PeriodicClock {

} // namespace grnxx

#endif // GRNXX_TIME_PERIODIC_CLOCK_HPP
#endif // GRNXX_PERIODIC_CLOCK_HPP
6 changes: 3 additions & 3 deletions lib/grnxx/stopwatch.hpp
Expand Up @@ -15,8 +15,8 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef GRNXX_TIME_STOPWATCH_HPP
#define GRNXX_TIME_STOPWATCH_HPP
#ifndef GRNXX_STOPWATCH_HPP
#define GRNXX_STOPWATCH_HPP

#include "grnxx/features.hpp"

Expand Down Expand Up @@ -58,4 +58,4 @@ class Stopwatch {

} // namespace grnxx

#endif // GRNXX_TIME_STOPWATCH_HPP
#endif // GRNXX_STOPWATCH_HPP
187 changes: 187 additions & 0 deletions lib/grnxx/string_builder2.cpp
@@ -0,0 +1,187 @@
/*
Copyright (C) 2012-2013 Brazil, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "grnxx/string_builder.hpp"

#include <cmath>

#include "grnxx/intrinsic.hpp"

namespace grnxx {

StringBuilder::StringBuilder(size_t size, StringBuilderFlags flags)
: buf_((size != 0) ? (new (std::nothrow) char[size]) : nullptr),
begin_(buf_.get()),
end_(buf_ ? (begin_ + size - 1) : nullptr),
ptr_(begin_),
flags_(flags),
failed_(!buf_) {
if (buf_) {
*ptr_ = '\0';
}
}

bool StringBuilder::resize_buf(size_t size) {
if (size < STRING_BUILDER_BUF_SIZE_MIN) {
size = STRING_BUILDER_BUF_SIZE_MIN;
} else {
size = size_t(1) << (bit_scan_reverse(size - 1) + 1);
}

std::unique_ptr<char[]> new_buf(new (std::nothrow) char[size]);
if (!new_buf) {
return false;
}

const size_t length = ptr_ - begin_;
std::memcpy(new_buf.get(), begin_, length);
ptr_ = new_buf.get() + length;
begin_ = new_buf.get();
end_ = new_buf.get() + size - 1;
buf_ = std::move(new_buf);
return true;
}

StringBuilder &operator<<(StringBuilder &builder, long long value) {
if (!builder) {
return builder;
}

char buf[32];
char *ptr = buf;
char *left = ptr;
if (value >= 0) {
do {
*ptr++ = static_cast<char>('0' + (value % 10));
value /= 10;
} while (value != 0);
} else {
*ptr++ = '-';
++left;

do {
// C++11 always rounds the result toward 0.
*ptr++ = static_cast<char>('0' - (value % 10));
value /= 10;
} while (value != 0);
}

char *right = ptr - 1;
while (left < right) {
using std::swap;
swap(*left++, *right--);
}

return builder.append(buf, ptr - buf);
}
StringBuilder &operator<<(StringBuilder &builder, unsigned long long value) {
if (!builder) {
return builder;
}

char buf[32];
char *ptr = buf;
do {
*ptr++ = static_cast<char>('0' + (value % 10));
value /= 10;
} while (value != 0);

char *left = buf;
char *right = ptr - 1;
while (left < right) {
using std::swap;
swap(*left++, *right--);
}

return builder.append(buf, ptr - buf);
}

StringBuilder &operator<<(StringBuilder &builder, float value) {
return builder << static_cast<double>(value);
}

StringBuilder &operator<<(StringBuilder &builder, double value) {
if (!builder) {
return builder;
}

switch (std::fpclassify(value)) {
case FP_NORMAL:
case FP_SUBNORMAL:
case FP_ZERO: {
break;
}
case FP_INFINITE: {
if (value > 0) {
return builder.append("inf", 3);
} else {
return builder.append("-inf", 4);
}
}
case FP_NAN:
default: {
return builder.append("nan", 3);
}
}

// The maximum value of double-precision floating point number (IEEE754)
// is 1.797693134862316E+308.
char buf[512];
int length = std::snprintf(buf, sizeof(buf), "%f", value);
if (length < 0) {
return builder.append("n/a", 3);
}
if (static_cast<size_t>(length) >= sizeof(buf)) {
length = sizeof(buf) - 1;
}
return builder.append(buf, length);
}

StringBuilder &operator<<(StringBuilder &builder, const void *value) {
if (!builder) {
return builder;
}
if (!value) {
return builder.append("nullptr", 7);
}

char buf[(sizeof(value) * 2) + 2];
buf[0] = '0';
buf[1] = 'x';

uintptr_t address = reinterpret_cast<uintptr_t>(value);
for (size_t i = 2; i < sizeof(buf); ++i) {
const uintptr_t digit = address >> ((sizeof(value) * 8) - 4);
buf[i] = static_cast<char>(
(digit < 10) ? ('0' + digit) : ('A' + digit - 10));
address <<= 4;
}
return builder.append(buf, sizeof(buf));
}

StringBuilder &operator<<(StringBuilder &builder, const Bytes &bytes) {
// TODO: StringBuilder should support const uint_8 *.
return builder.append(reinterpret_cast<const char *>(bytes.data()),
bytes.size());
}

StringBuilder &operator<<(StringBuilder &builder,
const std::exception &exception) {
return builder << "{ what = " << exception.what() << " }";
}

} // namespace grnxx

0 comments on commit 87208b8

Please sign in to comment.