MCPcopy Create free account
hub / github.com/VCVRack/Rack / truncate

Function truncate

src/string.cpp:80–97  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

78
79
80std::string truncate(const std::string& s, size_t maxCodepoints) {
81 if (s.empty() || maxCodepoints == 0)
82 return "";
83
84 size_t pos = 0;
85 for (size_t i = 0; i < maxCodepoints; i++) {
86 // If remaining bytes are less than remaining codepoints, the string can't possibly be truncated.
87 if (s.size() - pos <= maxCodepoints - i)
88 return s;
89
90 pos = UTF8NextCodepoint(s, pos);
91 // Check if at end
92 if (pos >= s.size())
93 return s;
94 }
95
96 return s.substr(0, pos);
97}
98
99
100std::string truncatePrefix(const std::string& s, size_t maxCodepoints) {

Callers 1

ellipsizeFunction · 0.85

Calls 3

UTF8NextCodepointFunction · 0.85
emptyMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected