#include <cstdio>
#include <cstring>
#include <iostream>
#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 '\b': out += "\\b"; break;
case '\f': out += "\\f"; 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"; }
std::string jint(long long v) { return std::to_string(v); }
std::string juint(unsigned long long v) { return std::to_string(v); }
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
int main() {
std::vector<lab::Case> cases;
// Case 1: C-style null-terminated char array vs literal
char site1[8] = {'B', 'L', 'U', 'E', 'S', 'K', 'Y', '\0'};
char site2[] = "BLUESKY";
bool ok1 = (sizeof(site2) == 8) && (site2[7] == '\0') && (std::strcmp(site1, site2) == 0);
cases.push_back({
"cstyle_null_terminated_array",
lab::esc("BLUESKY"),
lab::esc(std::string(site1)),
ok1
});
// Case 2: strcpy, strcat, strlen
char s1[20] = "bluesky";
char s2[20] = "cpp";
char s3[20];
std::strcpy(s3, s1);
std::strcat(s1, s2);
std::size_t len = std::strlen(s1);
bool ok2 = (std::strcmp(s3, "bluesky") == 0) && (std::strcmp(s1, "blueskycpp") == 0) && (len == 10);
cases.push_back({
"cstring_strcpy_strcat_strlen",
lab::esc("blueskycpp:10"),
lab::esc(std::string(s1) + ":" + std::to_string(len)),
ok2
});
// Case 3: strcmp, strchr, strstr
int cmp_eq = std::strcmp("abc", "abc");
int cmp_lt = std::strcmp("abc", "abd");
const char* p_ch = std::strchr(s1, 'k');
const char* p_sub = std::strstr(s1, "sky");
bool ok3 = (cmp_eq == 0) && (cmp_lt < 0) &&
(p_ch != nullptr && (p_ch - s1) == 5) &&
(p_sub != nullptr && (p_sub - s1) == 4);
cases.push_back({
"cstring_strcmp_strchr_strstr",
lab::jbool(true),
lab::jbool(ok3),
ok3
});
// Case 4: std::string copy, concat, size, find
std::string a = "bluesky";
std::string b = "cpp";
std::string c = a;
std::string joined = a + b;
bool ok4 = (c == "bluesky") && (joined == "blueskycpp") &&
(joined.size() == 10) && (joined.find("cpp") == 7);
cases.push_back({
"std_string_concat_and_methods",
lab::esc("blueskycpp:10"),
lab::esc(joined + ":" + std::to_string(joined.size())),
ok4
});
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-strings-lab/explore-cpp-strings.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-strings.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2397.zip