From babd3949f6559bee10c562dd4c6d41ae974f4b8e Mon Sep 17 00:00:00 2001 From: Arthur Ice Date: Fri, 10 May 2013 23:18:00 -0700 Subject: [PATCH] Added StringUtil h/cpp which contains StringFormat --- common/StringUtil.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++ common/StringUtil.h | 23 ++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 common/StringUtil.cpp create mode 100644 common/StringUtil.h diff --git a/common/StringUtil.cpp b/common/StringUtil.cpp new file mode 100644 index 000000000..2371d5148 --- /dev/null +++ b/common/StringUtil.cpp @@ -0,0 +1,81 @@ +/* + * Copyright 2013 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "StringUtil.h" +#include +#include +#include + +#ifdef _WINDOWS + #include + + #define snprintf _snprintf + #define strncasecmp _strnicmp + #define strcasecmp _stricmp +#else + #include +#endif + + +// original source: +// https://github.com/facebook/folly/blob/master/folly/String.cpp +// +void StringFormat(std::string& output, const char* format, ...) +{ + va_list args; + // Tru to the space at the end of output for our output buffer. + // Find out write point then inflate its size temporarily to its + // capacity; we will later shrink it to the size needed to represent + // the formatted string. If this buffer isn't large enough, we do a + // resize and try again. + + const auto write_point = output.size(); + auto remaining = output.capacity() - write_point; + output.resize(output.capacity()); + + va_start(args, format); + int bytes_used = vsnprintf(&output[write_point], remaining, format,args); + va_end(args); + if (bytes_used < 0) { + + std::string errorMessage("Invalid format string; snprintf returned negative with format string: "); + errorMessage.append(format); + + throw std::runtime_error(errorMessage); + } + else if ((unsigned int)bytes_used < remaining) { + // There was enough room, just shrink and return. + output.resize(write_point + bytes_used); + } + else { + output.resize(write_point + bytes_used + 1); + remaining = bytes_used + 1; + + va_start(args, format); + bytes_used = vsnprintf(&output[write_point], remaining, format, args); + va_end(args); + + if ((unsigned int)(bytes_used + 1) != remaining) { + + std::string errorMessage("vsnprint retry did not manage to work with format string: "); + errorMessage.append(format); + + throw std::runtime_error(errorMessage); + } + + output.resize(write_point + bytes_used); + } +} diff --git a/common/StringUtil.h b/common/StringUtil.h new file mode 100644 index 000000000..226aa82f2 --- /dev/null +++ b/common/StringUtil.h @@ -0,0 +1,23 @@ +/* + * Copyright 2013 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef _STRINGUTIL_H_ +#define _STRINGUTIL_H_ + +#include + +void StringFormat(std::string& output, const char* format, ...); + +#endif