Skip to content

Commit

Permalink
Allow FORMAT_STR to use signed integers
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandlo committed Nov 15, 2023
1 parent 19b6ec8 commit de5ccba
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 61 deletions.
3 changes: 1 addition & 2 deletions src/core/control/xojfile/LoadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,8 +1047,7 @@ void LoadHandler::parserText(GMarkupParseContext* context, const gchar* text, gs
} else {
g_warning("%s", FC(_F("xoj-File: {1}") % handler->filepath.string().c_str()));
g_warning("%s", FC(_F("Wrong number of pressure values, got {1}, expected {2}") %
as_signed(handler->pressureBuffer.size()) %
(as_signed(handler->stroke->getPointCount()) - 1)));
handler->pressureBuffer.size() % (handler->stroke->getPointCount() - 1)));
}
handler->pressureBuffer.clear();
}
Expand Down
9 changes: 4 additions & 5 deletions src/core/gui/SearchBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,10 @@ void SearchBar::search(Fun next) {
if (found) {
control->getScrollHandler()->scrollToPage(page, matchRect);
control->getScrollHandler();
gtk_label_set_text(
GTK_LABEL(lbSearchState),
(occurrences == 1 ? FC(_F("Text found once on page {1}") % (as_signed(page) + 1)) :
FC(_F("Text found {1} times on page {2} ({3} of {1})") %
as_signed(occurrences) % (as_signed(page) + 1) % as_signed(indexInPage))));
gtk_label_set_text(GTK_LABEL(lbSearchState),
(occurrences == 1 ? FC(_F("Text found once on page {1}") % (page + 1)) :
FC(_F("Text found {1} times on page {2} ({3} of {1})") %
occurrences % (page + 1) % indexInPage)));
return;
}
if (page == originalPage) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/gui/menus/menubar/RecentDocumentsSubmenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ auto createRecentMenuItem(const GtkRecentInfo* info, size_t i) {

// escape underscore
StringUtils::replaceAllChars(display_name, {replace_pair('_', "__")});
std::string label = FS(FORMAT_STR("{1}. {2}") % (as_signed(i) + 1) % display_name);
std::string label = FS(FORMAT_STR("{1}. {2}") % (i + 1) % display_name);

std::string action = G_ACTION_NAMESPACE;
action += OPEN_ACTION_NAME;
Expand Down
7 changes: 3 additions & 4 deletions src/core/gui/toolbarMenubar/ToolPageSpinner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "util/Assert.h" // for xoj_assert
#include "util/gtk4_helper.h" // for gtk_box_append
#include "util/i18n.h" // for FS, _, _F, C_F
#include "util/safe_casts.h" // for as_signed

ToolPageSpinner::ToolPageSpinner(std::string id, IconNameHelper iconNameHelper):
AbstractToolItem(std::move(id)),
Expand All @@ -29,12 +28,12 @@ void ToolPageSpinner::setPageInfo(const size_t pageCount, const size_t pdfPage)
}

void ToolPageSpinner::updateLabels() {
std::string ofString = FS(C_F("Page {pagenumber} \"of {pagecount}\"", " of {1}") % as_signed(this->pageCount));
std::string ofString = FS(C_F("Page {pagenumber} \"of {pagecount}\"", " of {1}") % this->pageCount);
if (this->pdfPage > 0) {
if (this->orientation == GTK_ORIENTATION_HORIZONTAL) {
ofString += std::string(", ") + FS(_F("PDF Page {1}") % as_signed(this->pdfPage));
ofString += std::string(", ") + FS(_F("PDF Page {1}") % this->pdfPage);
} else {
ofString += std::string("\n") + FS(_F("PDF {1}") % as_signed(this->pdfPage));
ofString += std::string("\n") + FS(_F("PDF {1}") % this->pdfPage);
}
gtk_label_set_text(GTK_LABEL(lbPageNo.get()), ofString.c_str());
}
Expand Down
38 changes: 0 additions & 38 deletions src/util/PlaceholderString.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,17 @@
#include "util/PlaceholderString.h"

#include <cstddef> // for size_t
#include <exception> // for exception
#include <utility> // for move

#include <glib.h> // for g_error

#include "util/safe_casts.h" // for as_unsigned

/**
* Format String
*/
class PlaceholderElementString: public PlaceholderElement {
public:
explicit PlaceholderElementString(std::string text): text(std::move(text)) {}

auto format(std::string format) const -> std::string override { return text; }

private:
std::string text;
};

/**
* Format int
*/
class PlaceholderElementInt: public PlaceholderElement {
public:
explicit PlaceholderElementInt(int64_t value): value(value) {}

auto format(std::string format) const -> std::string override { return std::to_string(value); }

private:
int64_t value;
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

PlaceholderString::PlaceholderString(std::string text): text(std::move(text)) {}

auto PlaceholderString::operator%(int64_t value) -> PlaceholderString& {
data.emplace_back(std::make_unique<PlaceholderElementInt>(value));
return *this;
}

auto PlaceholderString::operator%(std::string value) -> PlaceholderString& {
data.emplace_back(std::make_unique<PlaceholderElementString>(std::move(value)));
return *this;
}

auto PlaceholderString::formatPart(std::string format) const -> std::string {
std::string formatDef;

Expand Down
54 changes: 47 additions & 7 deletions src/util/include/util/PlaceholderString.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

#pragma once

#include <cstdint> // for int64_t
#include <memory> // for unique_ptr
#include <ostream> // for ostream
#include <string> // for string
#include <vector> // for vector
#include <cstddef> // for size_t
#include <cstdint> // for int64_t
#include <memory> // for unique_ptr
#include <ostream> // for ostream
#include <string> // for string
#include <utility> // for move
#include <vector> // for vector

#include <glib.h> // for g_error

/**
* Base class for Formatting
Expand All @@ -34,6 +38,35 @@ class PlaceholderElement {
virtual auto format(std::string format) const -> std::string = 0;
};


/**
* Format String
*/
class PlaceholderElementString: public PlaceholderElement {
public:
explicit PlaceholderElementString(std::string text): text(std::move(text)) {}

auto format(std::string format) const -> std::string override { return text; }

private:
std::string text;
};

/**
* Format int
*/
template <typename T>
class PlaceholderElementInt: public PlaceholderElement {
public:
explicit PlaceholderElementInt(T value): value(value) {}

auto format(std::string format) const -> std::string override { return std::to_string(value); }

private:
T value;
};


/**
* Placeholder String, used for formatting. Support Placeholder like
* {1}, {2} etc. Use {{ for {
Expand All @@ -42,8 +75,15 @@ struct PlaceholderString {
PlaceholderString(std::string text);

// Placeholder methods
PlaceholderString& operator%(int64_t value);
PlaceholderString& operator%(std::string value);
template <typename T>
auto operator%(T value) -> PlaceholderString& {
if constexpr (std::is_integral_v<T>) {
data.emplace_back(std::make_unique<PlaceholderElementInt<T>>(value));
} else {
data.emplace_back(std::make_unique<PlaceholderElementString>(std::move(value)));
}
return *this;
}

// Process Method
std::string str() const;
Expand Down
8 changes: 4 additions & 4 deletions src/util/serializing/ObjectInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,16 @@ auto ObjectInputStream::readImage() -> std::string {

void ObjectInputStream::checkType(char type) {
if (istream.str().size() < 2) {
throw InputStreamException(FS(FORMAT_STR("End reached, but try to read {1}, index {2} of {3}") % getType(type) %
(uint32_t)pos() % (uint32_t)len),
__FILE__, __LINE__);
throw InputStreamException(
FS(FORMAT_STR("End reached, but try to read {1}, index {2} of {3}") % getType(type) % pos() % len),
__FILE__, __LINE__);
}
char t = 0, underscore = 0;
istream >> underscore >> t;

if (underscore != '_') {
throw InputStreamException(FS(FORMAT_STR("Expected type signature of {1}, index {2} of {3}, but read '{4}'") %
getType(type) % ((uint32_t)pos() + 1) % (uint32_t)len % underscore),
getType(type) % (pos() + 1) % len % underscore),
__FILE__, __LINE__);
}

Expand Down
15 changes: 15 additions & 0 deletions test/unit_tests/util/I18nTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,18 @@ TEST(UtilI18n, test3) {
string msg = FS(FORMAT_STR(" of {1}{2}") % 5 % 6);
EXPECT_EQ(std::string(" of 56"), msg);
}

TEST(UtilI18n, test16bit) {
string msg = FS(FORMAT_STR("{1} = {2} and {3}") % 60123 % 60123U % -65536);
EXPECT_EQ(std::string("60123 = 60123 and -65536"), msg);
}

TEST(UtilI18n, test32bit) {
string msg = FS(FORMAT_STR("{1} and {2}") % 4294967295U % -12345678);
EXPECT_EQ(std::string("4294967295 and -12345678"), msg);
}

TEST(UtilI18n, test64bit) {
string msg = FS(FORMAT_STR("{1} and {2}") % 1234567890123456789U % -1234567890123456789);
EXPECT_EQ(std::string("1234567890123456789 and -1234567890123456789"), msg);
}

0 comments on commit de5ccba

Please sign in to comment.