蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

实测 <locale> 经典 C 区域、自定义 numpunct 千分位、collate 比较与 put_time/ctype

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-libs-locale.cpp
explore-cpp-libs-locale.cpp115 行
#include <cstdio>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <locale>
#include <sstream>
#include <string>
#include <vector>

namespace lab {

struct Case {
    std::string name;
    std::string expected_json;
    std::string actual_json;
    bool passed;
};

std::string esc(const std::string& s) {
    std::string out = "\"";
    for (unsigned char c : s) {
        switch (c) {
            case '\"': out += "\\\""; break;
            case '\\': out += "\\\\"; break;
            case '\n': out += "\\n"; break;
            case '\r': out += "\\r"; break;
            case '\t': out += "\\t"; break;
            default:
                if (c < 0x20) {
                    char buf[8];
                    std::snprintf(buf, sizeof(buf), "\\u%04x", c);
                    out += buf;
                } else {
                    out += static_cast<char>(c);
                }
        }
    }
    out += "\"";
    return out;
}

std::string jbool(bool v) { return v ? "true" : "false"; }

void emit(const std::vector<Case>& cases) {
    std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
    for (std::size_t i = 0; i < cases.size(); ++i) {
        if (i) std::cout << ", ";
        std::cout << "{\"name\": " << esc(cases[i].name)
                  << ", \"expected\": " << cases[i].expected_json
                  << ", \"actual\": " << cases[i].actual_json
                  << ", \"passed\": " << jbool(cases[i].passed) << "}";
    }
    std::cout << "]}\n";
}

} // namespace lab

namespace {

struct CommaNumpunct : std::numpunct<char> {
protected:
    char do_thousands_sep() const override { return ','; }
    char do_decimal_point() const override { return '.'; }
    std::string do_grouping() const override { return "\3"; }
};

} // namespace

int main() {
    std::vector<lab::Case> cases;

    // Case 1: Classic "C" locale and custom numpunct thousand separator formatting
    std::locale c_loc("C");
    std::locale comma_loc(c_loc, new CommaNumpunct());
    std::ostringstream oss;
    oss.imbue(comma_loc);
    oss << std::fixed << std::setprecision(2) << 1234567.89;
    bool c1 = (c_loc.name() == "C") && (oss.str() == "1,234,567.89");
    cases.push_back({"classic_c_locale_and_numpunct", lab::jbool(true), lab::jbool(c1), c1});

    // Case 2: std::collate<char> string comparison under "C" locale
    const auto& coll = std::use_facet<std::collate<char>>(c_loc);
    std::string s1 = "apple";
    std::string s2 = "banana";
    int cmp_ab = coll.compare(s1.data(), s1.data() + s1.size(), s2.data(), s2.data() + s2.size());
    int cmp_aa = coll.compare(s1.data(), s1.data() + s1.size(), s1.data(), s1.data() + s1.size());
    bool c2 = (cmp_ab < 0) && (cmp_aa == 0);
    cases.push_back({"collate_string_comparison", lab::jbool(true), lab::jbool(c2), c2});

    // Case 3: Date/time formatting via std::put_time (C++ locale) and std::strftime (C locale)
    std::tm fixed_tm{};
    fixed_tm.tm_year = 2023 - 1900;
    fixed_tm.tm_mon = 2;   // March
    fixed_tm.tm_mday = 13; // Monday, March 13, 2023
    fixed_tm.tm_wday = 1;  // Monday
    std::ostringstream dt_oss;
    dt_oss.imbue(c_loc);
    dt_oss << std::put_time(&fixed_tm, "%A, %B %d, %Y");
    char cbuf[64] = {};
    std::strftime(cbuf, sizeof(cbuf), "%A, %B %d, %Y", &fixed_tm);
    bool c3 = (dt_oss.str() == "Monday, March 13, 2023") && (std::string(cbuf) == "Monday, March 13, 2023");
    cases.push_back({"date_time_put_time_and_strftime", lab::jbool(true), lab::jbool(c3), c3});

    // Case 4: std::ctype<char> character classification and case conversion
    const auto& ct = std::use_facet<std::ctype<char>>(c_loc);
    bool c4 = ct.is(std::ctype_base::alpha, 'A') &&
              ct.is(std::ctype_base::digit, '7') &&
              (ct.toupper('m') == 'M') &&
              (ct.tolower('Z') == 'z');
    cases.push_back({"ctype_case_conversion", lab::jbool(true), lab::jbool(c4), c4});

    lab::emit(cases);
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。

入口文件:projects/cplusplus-cpp-libs-locale-lab/explore-cpp-libs-locale.cpp

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-cpp-libs-locale.cpp,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C++

课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip

下载课程关联工程包:C++

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2356.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习