str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem…
- User
str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It w as popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
str = wordWrap(str, 40);
function wordWrap(str, maxWidth) { var newLineStr = "\n"; done = false; res = ''; while (str.length > maxWidth) {
found = false; // Inserts new line at first whitespace of the line for (i = maxWidth - 1; i >= 0; i--) { if (testWhite(str.charAt(i))) { res = res + [str.slice(0, i), newLineStr].join(''); str = str.slice(i + 1); found = true; break; } } // Inserts new line at maxWidth position, the word is too long to wrap if (!found) { res += [str.slice(0, maxWidth), newLineStr].join(''); str = str.slice(maxWidth); }} return res + str;}
function testWhite(x) { var white = new RegExp(/^\s$/); return white.test(x.charAt(0)); };
перепиши на lua
- Assistant
Certainly! Below is the equivalent function in Lua that performs word wrapping:
function wordWrap(str, maxWidth) local newLineStr = "\n" local done = false local res = "" while #str > maxWidth do local found = false -- Find the first whitespace within the maximum width for i = maxWidth, 1, -1 do if testWhite(str:sub(i, i)) then res = res .. str:sub(1, i - 1) .. newLineStr str = str:sub(i + 1) found = true break end end -- Insert a new line at maxWidth if no whitespace was found if not found then res = res .. str:sub(1, maxWidth) .. newLineStr str = str:sub(maxWidth + 1) end end return res .. str end function testWhite(x) return x:match("^%s") ~= nil end -- Example usage local str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." print(wordWrap(str, 40)) - User
можно ли модифицировать эту функцию так чтобы она возвращала количество переносов?
- Assistant
Sure! To modify the
wordWrapfunction to return the number of line breaks (or wraps) it performs, you can introduce a counter variable to keep track of the number of new lines added. Here's the modified Lua code:function wordWrap(str, maxWidth) local newLineStr = "\n" local res = "" local lineBreakCount = 0 while #str > maxWidth do local found = false -- Find the first whitespace within the maximum width for i = maxWidth, 1, -1 do if testWhite(str:sub(i, i)) then res = res .. str:sub(1, i - 1) .. newLineStr str = str:sub(i + 1) found = true lineBreakCount = lineBreakCount + 1 break end end -- Insert a new line at maxWidth if no whitespace was found if not found then res = res .. str:sub(1, maxWidth) .. newLineStr str = str:sub(maxWidth + 1) lineBreakCount = lineBreakCount + 1 end end return res .. str, lineBreakCount end function testWhite(x) return x:match("^%s") ~= nil end -- Example usage local str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." local wrappedStr, numOfLineBreaks = wordWrap(str, 40) print(wrappedStr) print("Number of line breaks:", numOfLineBreaks)