Jak wyrazić dosłownie znak "{" w fmt?

0

Nie ma pytana, doczytałem początek https://fmt.dev/latest/syntax.html ;)

std::string _error{"'{{'"}; // nie może być pojedynczny { !!!
auto _str = fmt::format("{}", _error);
fmt::print(_str);

Czy tekst { jest jakiś magiczny ?
Bo fmt::format nie daje z nim rady ?
Bug czy jakiś future i zrzucamy to na karb niedoświadczenia w c++ ?

#include <fmt/color.h> 
int main()
{
    // orginalna wersja tekstu (blad podczas parsowania JSON)
    // std::string _error{"[json.exception.parse_error.101] parse error at line 1, column 3: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal"};
    
    // wyizolowany blad do trzech znaków 
    std::string _error{"'{'"};
    auto _str = fmt::format("{}", _error);
    fmt::print(_str);
    
    // auto _errorRed = fmt::format(bg(fmt::color::red), _error);
}

https://godbolt.org/z/dbxsdPa6o

2

Ok częściowo ogarnąłeś problem, ale troszkę to niepokojące, że używasz fmt do zbudowania format string-u dla kolejnego wywołania fmt.
Ergo twoje pytanie cierpi na problem XY.

0

Chciałem zrobić mały wodotrysk w logowaniu na konsole
image

catch(const std::exception& e)
{
    auto _filename = fmt::format(bg(fmt::color::green), fname);
    auto _error = fmt::format(bg(fmt::color::red), e.what());
    spdlog::error("Error load file:{} message:{}", _filename, _error);
}

w e.what() był tekst który zawierał { i się pięknie posypało

Pozostaje pytanie jak przerobić tekst i zamienić klamry na podwójne , bo tak na szybko to w standardzie chyba nie widzę tego typu funkcji

Znalazlem taki replaceAll:


// https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
    if(from.empty())
        return;
    size_t start_pos = 0;
    while((start_pos = str.find(from, start_pos)) != std::string::npos) {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length(); 
    }
}

std::string _error{"[json.exception.parse_error.101] parse error at line 1, column 3: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal"};
    replaceAll(_error,"{","{{");
    replaceAll(_error,"}","}}");


4

API Reference — fmt 9.1.0 documentation

template<typename T>
auto fmt::styled(const T &value, text_style ts) -&gt; detail::styled_arg<remove_cvref_t<T>>

Returns an argument that will be formatted using ANSI escape sequences, to be used in a formatting function.

Example:

fmt::print("Elapsed time: {0:.2f} seconds",
           fmt::styled(1.23, fmt::fg(fmt::color::green) |
                                     fmt::bg(fmt::color::blue)));
    } catch (const std::exception& e) {
        fmt::print("Error load file:{} message:{}\n",
            fmt::styled(fname, bg(fmt::color::green)),
            fmt::styled(e.what(), bg(fmt::color::red)));
    }

https://godbolt.org/z/1xzfGr5dY

Szkoda, że nie ma opcji kolorowania z poziomu format string, byłoby to wygodniejsze i bardziej praktyczne.

1

Dziękuje , za funkcje której szukałem fmt::styled

Bazowałem na takich przykładach kolorowania
https://hackingcpp.com/cpp/libs/fmt.html#terminal-styles

0

Z czasów języka C została mi awersja do konstrukcji typu

printf(str);

fmt::print oczekuje, że pierwszym argumentem będzie ciąg formatujący, więc

fmt::print("{}", _str);
0

@MarekR22 , przyjrzałem się krytycznie , rzeczywiście to był problem XY
W ramach pokuty przed rozpoczęciem kolejnego wątku przeczytam 3x problem XY

1 użytkowników online, w tym zalogowanych: 0, gości: 1