Skip to content

Commit

Permalink
Use simple format function
Browse files Browse the repository at this point in the history
Remove Boost Format library usage.
  • Loading branch information
thezbyg committed Jul 6, 2018
1 parent c7c6bac commit d2b4f9f
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 76 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Expand Up @@ -82,6 +82,11 @@ set_compile_options(color)
target_link_libraries(color PUBLIC math)
target_include_directories(color PUBLIC source)

file(GLOB FORMAT_SOURCES source/Format.cpp source/Format.h)
add_library(format ${FORMAT_SOURCES})
set_compile_options(format)
target_include_directories(format PUBLIC source)

file(GLOB DYNV_SOURCES source/dynv/*.cpp source/dynv/*.h)
add_library(dynv ${DYNV_SOURCES})
set_compile_options(dynv)
Expand Down Expand Up @@ -137,6 +142,7 @@ target_link_libraries(gpick PUBLIC
dynv
lua
parser
format
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Lua_LIBRARIES}
Expand All @@ -160,6 +166,7 @@ target_link_libraries(tests PUBLIC
dynv
lua
parser
format
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
${Lua_LIBRARIES}
${Expat_LIBRARIES}
Expand Down
10 changes: 7 additions & 3 deletions source/BrightnessDarkness.cpp
Expand Up @@ -39,8 +39,8 @@
#include "layout/Layouts.h"
#include "layout/Style.h"
#include "StandardMenu.h"
#include "Format.h"
#include <gdk/gdkkeysyms.h>
#include <boost/format.hpp>
#include <math.h>
#include <string.h>
#include <sstream>
Expand Down Expand Up @@ -82,6 +82,10 @@ struct BrightnessDarknessColorNameAssigner: public ToolColorNameAssigner
return m_stream.str();
}
};
static string format(char prefix, int index)
{
return prefix + as_string(index);
}
static void calc(BrightnessDarknessArgs *args, bool preview, bool save_settings)
{
double brightness = gtk_range_2d_get_x(GTK_RANGE_2D(args->brightness_darkness));
Expand All @@ -99,15 +103,15 @@ static void calc(BrightnessDarknessArgs *args, bool preview, bool save_settings)
color_copy(&hsl_orig, &hsl);
hsl.hsl.lightness = mix_float(hsl.hsl.lightness, mix_float(hsl.hsl.lightness, 1, brightness), i / 4.0); //clamp_float(hsl.hsl.lightness + brightness / 8.0 * i, 0, 1);
color_hsl_to_rgb(&hsl, &r);
name = boost::str(boost::format("b%d") % i);
name = format('b', i);
box = args->layout_system->GetNamedBox(name.c_str());
if (box && box->style){
color_copy(&r, &box->style->color);
}
color_copy(&hsl_orig, &hsl);
hsl.hsl.lightness = mix_float(hsl.hsl.lightness, mix_float(hsl.hsl.lightness, 0, darkness), i / 4.0); //clamp_float(hsl.hsl.lightness - darkness / 8.0 * i, 0, 1);
color_hsl_to_rgb(&hsl, &r);
name = boost::str(boost::format("c%d") % i);
name = format('c', i);
box = args->layout_system->GetNamedBox(name.c_str());
if (box && box->style){
color_copy(&r, &box->style->color);
Expand Down
14 changes: 2 additions & 12 deletions source/ClosestColors.cpp
Expand Up @@ -33,8 +33,8 @@
#include "color_names/ColorNames.h"
#include "StandardMenu.h"
#include "Clipboard.h"
#include "Format.h"
#include <gdk/gdkkeysyms.h>
#include <boost/format.hpp>
#include <sstream>
using namespace std;

Expand Down Expand Up @@ -110,23 +110,13 @@ static void on_color_edit(GtkWidget *widget, ClosestColorsArgs *args)
}
color_object->release();
}
static boost::format format_ignore_arg_errors(const std::string &f_string)
{
boost::format fmter(f_string);
fmter.exceptions(boost::io::all_error_bits ^ (boost::io::too_many_args_bit | boost::io::too_few_args_bit));
return fmter;
}
static string identify_color_widget(GtkWidget *widget, ClosestColorsArgs *args)
{
if (args->color == widget){
return _("target");
}else for (int i = 0; i < 9; ++i){
if (args->closest_colors[i] == widget){
try{
return (format_ignore_arg_errors(_("match %d")) % (i + 1)).str();
}catch(const boost::io::format_error &e){
return (format_ignore_arg_errors("match %d") % (i + 1)).str();
}
return format(_("match {}"), i + 1);
}
}
return "unknown";
Expand Down
21 changes: 3 additions & 18 deletions source/ColorMixer.cpp
Expand Up @@ -38,8 +38,8 @@
#include "color_names/ColorNames.h"
#include "StandardMenu.h"
#include "Clipboard.h"
#include "Format.h"
#include <gdk/gdkkeysyms.h>
#include <boost/format.hpp>
#include <math.h>
#include <string.h>
#include <sstream>
Expand Down Expand Up @@ -204,30 +204,15 @@ static void on_color_edit(GtkWidget *widget, ColorMixerArgs *args)
color_object->release();
}

static boost::format format_ignore_arg_errors(const std::string &f_string)
{
boost::format fmter(f_string);
fmter.exceptions(boost::io::all_error_bits ^ (boost::io::too_many_args_bit | boost::io::too_few_args_bit));
return fmter;
}

static string identify_color_widget(GtkWidget *widget, ColorMixerArgs *args)
{
if (args->secondary_color == widget){
return _("secondary");
}else for (int i = 0; i < MAX_COLOR_LINES; ++i){
if (args->color[i].input == widget){
try{
return (format_ignore_arg_errors(_("primary %d")) % (i + 1)).str();
}catch(const boost::io::format_error &e){
return (format_ignore_arg_errors("primary %d") % (i + 1)).str();
}
return format(_("primary {}"), i + 1);
}else if (args->color[i].output == widget){
try{
return (format_ignore_arg_errors(_("result %d")) % (i + 1)).str();
}catch(const boost::io::format_error &e){
return (format_ignore_arg_errors("result %d") % (i + 1)).str();
}
return format(_("result {}"), i + 1);
}
}
return "unknown";
Expand Down
31 changes: 31 additions & 0 deletions source/Format.cpp
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2009-2018, Albertas Vyšniauskas
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* * Neither the name of the software author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "Format.h"
template<> std::string as_string<const std::string &>(const std::string &value)
{
return value;
}
template<> std::string as_string<const char*>(const char* value)
{
return value;
}
template<> std::string as_string<int>(int value)
{
return std::to_string(value);
}
69 changes: 69 additions & 0 deletions source/Format.h
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2009-2018, Albertas Vyšniauskas
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* * Neither the name of the software author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef GPICK_FORMAT_H_
#define GPICK_FORMAT_H_
#include <string>
#include <vector>
template<typename T>
std::string as_string(T value)
{
return std::to_string(value);
}
template<> std::string as_string<const std::string &>(const std::string &value);
template<> std::string as_string<const char*>(const char* value);
template<> std::string as_string<int>(int value);
template<typename ...Args>
std::string format(const char *format, const Args&... args)
{
std::vector<std::string> values = { as_string(args)... };
size_t max_length = 0;
for (auto &v: values){
max_length += v.length();
}
char previous_char = 0;
size_t i;
for (i = 0; format[i]; ++i){
if (format[i] == '}' && previous_char == '{'){
max_length -= 2;
}
previous_char = format[i];
}
max_length += i;
std::string result;
result.reserve(max_length);
previous_char = 0;
size_t argument_index = 0;
for (i = 0; format[i]; ++i){
if (format[i] == '}' && previous_char == '{'){
result.pop_back();
if (argument_index < values.size()){
auto &value = values[argument_index];
for (size_t j = 0; j < value.length(); ++j){
result.push_back(value[j]);
}
}
argument_index++;
}else{
result.push_back(format[i]);
}
previous_char = format[i];
}
return result;
}
#endif /* GPICK_FORMAT_H_ */
2 changes: 1 addition & 1 deletion source/SConscript
Expand Up @@ -77,7 +77,7 @@ executable = local_env.Program('gpick', source = [objects])
test_env = local_env.Clone()
test_env.Append(LIBS = ['boost_unit_test_framework'], CPPDEFINES = ['BOOST_TEST_DYN_LINK'])

tests = test_env.Program('tests', source = test_env.Glob('test/*.cpp') + [object_map['Color'], object_map['MathUtil'], object_map['lua/Script']] + dynv_objects + text_file_parser_objects)
tests = test_env.Program('tests', source = test_env.Glob('test/*.cpp') + [object_map['Color'], object_map['MathUtil'], object_map['lua/Script'], object_map['Format']] + dynv_objects + text_file_parser_objects)

Return('executable', 'tests', 'generated_files')

21 changes: 3 additions & 18 deletions source/Variations.cpp
Expand Up @@ -38,8 +38,8 @@
#include "color_names/ColorNames.h"
#include "StandardMenu.h"
#include "Clipboard.h"
#include "Format.h"
#include <gdk/gdkkeysyms.h>
#include <boost/format.hpp>
#include <math.h>
#include <string.h>
#include <sstream>
Expand Down Expand Up @@ -110,13 +110,6 @@ struct VariationsColorNameAssigner: public ToolColorNameAssigner
}
};

static boost::format format_ignore_arg_errors(const std::string &f_string) {
boost::format fmter(f_string);
fmter.exceptions(boost::io::all_error_bits ^ (boost::io::too_many_args_bit | boost::io::too_few_args_bit));
return fmter;
}


static int set_rgb_color(VariationsArgs *args, ColorObject* color, uint32_t color_index);
static int set_rgb_color_by_widget(VariationsArgs *args, ColorObject* color, GtkWidget* color_widget);

Expand Down Expand Up @@ -223,21 +216,13 @@ static string identify_color_widget(GtkWidget *widget, VariationsArgs *args)
return _("all colors");
}else for (int i = 0; i < MAX_COLOR_LINES; ++i){
if (args->color[i].color == widget){
try{
return (format_ignore_arg_errors(_("primary %d")) % (i + 1)).str();
}catch(const boost::io::format_error &e){
return (format_ignore_arg_errors("primary %d") % (i + 1)).str();
}
return format(_("primary {}"), i + 1);
}
for (int j = 0; j <= VAR_COLOR_WIDGETS; ++j){
if (args->color[i].var_colors[j] == widget){
if (j > VAR_COLOR_WIDGETS / 2)
j--;
try{
return (format_ignore_arg_errors(_("result %d line %d")) % (j + 1) % (i + 1)).str();
}catch(const boost::io::format_error &e){
return (format_ignore_arg_errors("result %d line %d") % (j + 1) % (i + 1)).str();
}
return format(_("result {} line {}"), j + 1, i + 1);
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions source/test/Format.cpp
@@ -0,0 +1,30 @@
#include <boost/test/unit_test.hpp>
#include "Format.h"
BOOST_AUTO_TEST_CASE(format_empty)
{
BOOST_CHECK_EQUAL(format(""), "");
}
BOOST_AUTO_TEST_CASE(format_missing_value)
{
BOOST_CHECK_EQUAL(format("{}"), "");
}
BOOST_AUTO_TEST_CASE(format_useless_value)
{
BOOST_CHECK_EQUAL(format("", 123), "");
}
BOOST_AUTO_TEST_CASE(format_int)
{
BOOST_CHECK_EQUAL(format("Hello world {}", 123), "Hello world 123");
}
BOOST_AUTO_TEST_CASE(format_string)
{
BOOST_CHECK_EQUAL(format("Hello {}", "world"), "Hello world");
}
BOOST_AUTO_TEST_CASE(format_mixed)
{
BOOST_CHECK_EQUAL(format("Hello {} {}", "world", 123), "Hello world 123");
}
BOOST_AUTO_TEST_CASE(format_unterminated)
{
BOOST_CHECK_EQUAL(format("Hello {", 123), "Hello {");
}
28 changes: 4 additions & 24 deletions source/uiListPalette.cpp
Expand Up @@ -30,7 +30,7 @@
#include "DynvHelpers.h"
#include "Vector2.h"
#include "I18N.h"
#include <boost/format.hpp>
#include "Format.h"
#include <sstream>
#include <iostream>
#include <iomanip>
Expand Down Expand Up @@ -109,12 +109,6 @@ static void find_selection_bounds(GtkTreeModel *model, GtkTreePath *path, GtkTre
args->last_index = index;
}

static boost::format format_ignore_arg_errors(const std::string &f_string) {
boost::format fmter(f_string);
fmter.exceptions(boost::io::all_error_bits ^ (boost::io::too_many_args_bit | boost::io::too_few_args_bit));
return fmter;
}

static void update_counts(ListPaletteArgs *args){
stringstream s;
GtkTreeSelection *sel;
Expand Down Expand Up @@ -150,31 +144,17 @@ static void update_counts(ListPaletteArgs *args){
}else{
s << bounds.min_index;
}

#ifdef ENABLE_NLS
string selected_color_count;
try{
selected_color_count = (format_ignore_arg_errors(ngettext("%d color", "%d colors", selected_count)) % selected_count).str();
}catch(const boost::io::format_error &e){
selected_color_count = ngettext("%d color", "%d colors", selected_count);
}
s << " (" << selected_color_count << ")";
s << " (" << format(ngettext("{} color", "{} colors", selected_count), selected_count) << ")";
#else
s << " (" << ((selected_count == 1) ? "color" : "colors") << ")";
#endif
s << " " << _("selected") << ". ";
}

#ifdef ENABLE_NLS
string total_color_count;
try{
total_color_count = (format_ignore_arg_errors(ngettext("Total %d color", "Total %d colors", total_colors)) % total_colors).str();
}catch(const boost::io::format_error &e){
total_color_count = ngettext("Total %d color", "Total %d colors", total_colors);
}
s << total_color_count;
s << format(ngettext("Total {} color", "Total {} colors", total_colors), total_colors);
#else
s << "Total " << total_colors << " colors.";
s << "Total " << total_colors << ((total_colors == 1) ? " color" : " colors");
#endif
gtk_label_set_text(GTK_LABEL(args->count_label), s.str().c_str());
}
Expand Down

0 comments on commit d2b4f9f

Please sign in to comment.