Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions common/common.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
##testing if devin picks up changes
Copy link
Author

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 changes

Or simply remove this line entirely since it seems to be for testing purposes only.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build Error: This line causes a compilation failure:

/home/ubuntu/repos/llama.cpp/common/common.cpp:1:1: error: stray '##' in program
    1 | ##testing if devin picks up changes
      | ^~
/home/ubuntu/repos/llama.cpp/common/common.cpp:1:3: error: 'testing' does not name a type

## is not a valid C++ comment syntax. This appears to be a test artifact that must be removed before merge. Use // for single-line comments or /* */ for multi-line comments in C++.


#if defined(_MSC_VER)
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
#endif
Expand Down Expand Up @@ -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";
}
Expand Down
3 changes: 3 additions & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API Consistency Issue: The function signature uses const std::string & while similar string utility functions in this same file use std::string_view:

// 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 when they have a string_view or string literal, consider changing the signature to:

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 max_len is a byte count, not a character count. For example:

// Truncate a string to a maximum byte length, adding ellipsis if truncated

This makes the behavior explicit and avoids surprising callers who might expect character-based truncation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Convention: Consider using std::string_view instead of const std::string & for consistency with nearby string utility functions in this file.

For example, string_ends_with and string_find_partial_stop (lines 586-588 in common.h) already use std::string_view:

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 std::string just to truncate, and maintains consistency with the existing API style in this module.


bool string_parse_kv_override(const char * data, std::vector<llama_model_kv_override> & overrides);
void string_process_escapes(std::string & input);

Expand Down
Loading