-
Notifications
You must be signed in to change notification settings - Fork 1
Add string_truncate utility function #328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| ##testing if devin picks up changes | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Build Error: This line causes a compilation failure:
|
||
|
|
||
| #if defined(_MSC_VER) | ||
| #define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING | ||
| #endif | ||
|
|
@@ -527,6 +529,37 @@ std::string string_repeat(const std::string & str, size_t n) { | |
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * @brief Truncates a string to a maximum length, appending ellipsis if truncated. | ||
| * | ||
| * This function shortens a string to fit within a specified maximum length. | ||
| * If truncation occurs, "..." is appended to indicate the string was shortened. | ||
| * | ||
| * @param str The input string to truncate. | ||
| * @param max_len The maximum allowed length of the output string (including ellipsis). | ||
| * | ||
| * @return The original string if its length is <= max_len. | ||
| * If max_len < 3, returns the first max_len characters without ellipsis. | ||
| * Otherwise, returns the first (max_len - 3) characters followed by "...". | ||
| * | ||
| * @note This function operates on bytes, not Unicode code points. Truncating | ||
| * UTF-8 strings may result in invalid sequences if cut mid-character. | ||
| * | ||
| * @example | ||
| * string_truncate("Hello World", 8) -> "Hello..." | ||
| * string_truncate("Hi", 10) -> "Hi" | ||
| * string_truncate("Hello", 2) -> "He" | ||
| */ | ||
| std::string string_truncate(const std::string & str, size_t max_len) { | ||
| if (str.length() <= max_len) { | ||
| return str; | ||
| } | ||
| if (max_len < 3) { | ||
| return str.substr(0, max_len); | ||
| } | ||
| return str.substr(0, max_len - 3) + "..."; | ||
| } | ||
|
|
||
| std::string string_from(bool value) { | ||
| return value ? "true" : "false"; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -587,6 +587,9 @@ bool string_ends_with(const std::string_view & str, const std::string_view & suf | |
| bool string_remove_suffix(std::string & str, const std::string_view & suffix); | ||
| size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop); | ||
|
|
||
| // Truncate a string to a maximum length, adding ellipsis if truncated | ||
| std::string string_truncate(const std::string & str, size_t max_len); | ||
|
Comment on lines
+590
to
+591
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. API Consistency Issue: The function signature uses // Existing functions use string_view:
bool string_ends_with(const std::string_view & str, const std::string_view & suffix);
bool string_remove_suffix(std::string & str, const std::string_view & suffix);
size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop);
// New function uses string:
std::string string_truncate(const std::string & str, size_t max_len);Recommendation: For consistency with the existing codebase conventions and to avoid forcing callers to materialize a std::string string_truncate(std::string_view str, size_t max_len);Additionally, since the PR description already notes the UTF-8 truncation concern, it would be helpful to clarify in the doc comment that // Truncate a string to a maximum byte length, adding ellipsis if truncatedThis makes the behavior explicit and avoids surprising callers who might expect character-based truncation.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code Convention: Consider using For example, bool string_ends_with(const std::string_view & str, const std::string_view & suffix);
size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop);Suggested change: std::string string_truncate(std::string_view str, size_t max_len);This avoids forcing callers to allocate a |
||
|
|
||
| bool string_parse_kv_override(const char * data, std::vector<llama_model_kv_override> & overrides); | ||
| void string_process_escapes(std::string & input); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Build Blocker: This line is not valid C++ syntax and will cause compilation to fail with an error like
stray '##' in program.This appears to be a test comment that should be removed before merging. If you intended to add a comment, use
//for single-line comments:// testing if devin picks up changesOr simply remove this line entirely since it seems to be for testing purposes only.