write c++ function for line wrapping that also returns the number of line breaks
- User
write c++ function for line wrapping that also returns the number of line breaks
- Assistant
- User
максимально короткую и простую функцию
- Assistant
Вот более короткая и простая версия функции для переноса текста, которая также возвращает число переносов строк:
#include <iostream> #include <string> std::pair<std::string, int> wrapText(const std::string& text, int maxWidth) { std::string wrapped, current; int lineBreaks = 0; size_t pos = 0, prev = 0; while ((pos = text.find_first_of(' ', prev)) != std::string::npos) { std::string word = text.substr(prev, pos - prev); if (current.length() + word.length() + 1 > maxWidth) { wrapped += current + '\n'; current = word; lineBreaks++; } else { if (!current.empty()) current += ' '; current += word; } prev = pos + 1; } // Add the last word or line if (!text.substr(prev).empty()) { if (!current.empty() && current.length() + text.substr(prev).length() + 1 > maxWidth) { wrapped += current + '\n'; current = text.substr(prev); lineBreaks++; } else { if (!current.empty()) current += ' '; current += text.substr(prev); } } if (!current.empty()) { wrapped += current; } return {wrapped, lineBreaks}; } int main() { std::string text = "This is an example of text that needs to be wrapped based on a specific maximum width."; int maxWidth = 20; auto result = wrapText(text, maxWidth); std::cout << "Wrapped Text:\n" << result.first << "\n"; std::cout << "Number of Line Breaks: " << result.second << "\n"; return 0; } - User
на луа
- Assistant
Вот упрощенная версия функции переноса текста на Lua, которая также возвращает количество переносов строк:
function wrapText(text, maxWidth) local wrapped = "" local currentLine = "" local lineBreaks = 0 for word in text:gmatch("%S+") do if #currentLine + #word + 1 > maxWidth then wrapped = wrapped .. currentLine .. "\n" currentLine = word lineBreaks = lineBreaks + 1 else if #currentLine > 0 then currentLine = currentLine .. " " end currentLine = currentLine .. word end end -- Добавляем последнюю строку, если есть if #currentLine > 0 then wrapped = wrapped .. currentLine end return wrapped, lineBreaks end -- Пример использования local text = "This is an example of text that needs to be wrapped based on a specific maximum width." local maxWidth = 20 local wrappedText, lineBreaks = wrapText(text, maxWidth) print("Wrapped Text:\n" .. wrappedText) print("Number of Line Breaks: " .. lineBreaks) - User
максимально короткую и простую насколько позволяет луа
- Assistant
Вот более короткая и простая версия функции для переноса текста на Lua:
function wrapText(text, maxWidth) local wrapped, lineBreaks, line = "", 0, "" for word in text:gmatch("%S+") do if #line + #word + 1 > maxWidth then wrapped = wrapped .. line .. "\n" line = word lineBreaks = lineBreaks + 1 else line = (line == "" and word) or (line .. " " .. word) end end if line ~= "" then wrapped = wrapped .. line end return wrapped, lineBreaks end -- Пример использования local text = "This is an example of text that needs to be wrapped based on a specific maximum width." local result, breaks = wrapText(text, 20) print("Wrapped Text:\n" .. result) print("Number of Line Breaks: " .. breaks)Краткое объяснение:
-
Сбор слов: Функция используется
gmatchи регулярным выражением"%S+"для извлечения отдельных слов из строки.
-