Lua:字符串中的换行符 [英] Lua: Line breaks in strings

查看:5868
本文介绍了Lua:字符串中的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一种格式化程序,该格式化程序将使用一个长字符串并将其格式化为在特定字符限制内的单词处折断的一系列行.例如,他吃面包,每8个字符中断一次,就会返回类似的内容:

I've been working on a formatter that will take a long string and format it into a series of lines broken off at the word within a certain character limit. For instance, He eats the bread, broken off at every 8 characters, would return something like:

He eats
the
bread

这是因为他吃的食物"包含7个字符,面包"包含9个字符,因此它必须在"the"处断开并继续"bread".

This is because "He eats" contains 7 characters, and "the bread" contains 9, so it has to break off at "the" and continue with "bread".

由于之前其他成员的帮助,脚本本身一直运行良好.但是,现在我面临着新的挑战.

The script itself has been working wonderfully, thanks to the help of other members before. However, now I have a new challenge.

我正在使用自由输入框输入一系列行.如果有人熟悉它,它适用于一个名为MUSHclient的程序. utils.editbox 打开一个编辑器文本框,并允许自由格式,并以 string 返回结果.例如:

I'm utilizing a free-entry box for entering the series of lines. It's for a program called MUSHclient, if anyone is familiar with it. The utils.editbox opens up an editor text box, and allows free form, and returns the result as a string. For example:

result = utils.editbox("What are you typing?")

将类型化的响应保存到string类型的result中.我想适当地插入换行符,但是我不知道该怎么做.如果我print(result),它将返回类似以下的内容:

saves the typed response into result which is a string type. I want to put line breaks in properly, but I can't figure out how to do that. If I print(result), it returns something like the following:

This is the result of three separate paragraphs.

As you can see, there is a line break between each paragraph.

So the result contains line breaks, or control characters.

现在,我已经使用以下代码设法推断了单独的行,但没有推断出它们之间的换行符:

Now, I've managed to extrapolate the individual lines, but not the linebreaks in between, using the following code:

for line in result:gmatch("[^%c]+%c?") do 
  Send(note_wrap(line))
end

如何推断换行符和文本内容?

How can I extrapolate for linebreaks as well as textual content?

编辑供参考,note_wrap功能如下:

function note_wrap(str, limit, indent, indent1)
  indent = indent or ""
  indent1 = indent1 or indent
  limit = limit or 79
  local here = 1-#indent1
  local last_color = ''
  return indent1..str:gsub("(%s+)()(%S+)()",
    function(sp, st, word, fi)
      local delta = 0
      local color_before_current_word = last_color
      word:gsub('()@([@%a])', 
        function(pos, c)
          if c == '@' then 
            delta = delta + 1 
          elseif c == 'x' then 
            delta = delta + 5
            last_color = word:sub(pos, pos+4)
          else                 
            delta = delta + 2 
            last_color = word:sub(pos, pos+1)
          end
        end)
      here = here + delta
      if fi-here > limit then
        here = st - #indent + delta
        return "\n"..indent..color_before_current_word..word
      end
    end)
end

使用此代码的原因是能够食用 @r他吃@ x123the面包,忽略颜色代码(由@< letter>或@ x< digits指示),然后返回上述结果:

The reason for this code is to be able to take @rHe eats @x123the bread, ignore the color codes (indicated by @<letter> or @x<digits>) and return the aforementioned result of:

He eats
the
bread

但是插入颜色代码,以便随后返回:

but insert the color codes so it then returns:

@rHe eats
@x123the
bread

如果可以对其进行修改以识别新行并放入实际的新行,那就太好了.

If this can be modified to recognize new lines and put an actual new line in, that would be fabulous.

编辑2 尝试了Paul的解决方案,但它没有满足我的要求.我可能还不够清楚,所以我将在此编辑中尝试将其清除.

Edit 2 Tried Paul's solution, and it's not doing what I'm needing it to. I may not have been clear enough, so I'll try to clear it up in this edit.

当我在自由格式框中键入内容时,我希望信息按原样显示,但是要正确设置格式才能保留信息并保持输入的换行符.例如,如果我在自由格式框中输入以下内容:

When I type into the free form box, I want the information presented exactly as is, but formatted to break correctly AND maintain entered newlines. For example, if I write this in the free form box:

Mary had a little lamb, little lamb, little lamb
Mary had a little lamb
It's fleece was white as snow

使用Paul的代码,并将字符串设置为79,我得到:

using Paul's code, and setting string to 79, I get:

Mary had a little lamb, little lamb, little lamb

Mary had a little lamb

It's fleece was white as snow

那不是我想要的.我希望它按原样返回它,并在必要时换行.因此,如果我限制为20个字符,它将返回:

And that's not what I want. I'd want it to return it just as it was written, and breaking line as necessary. So if I had a 20 character limit, it'd return:

Mary had a little
lamb, little lamb,
little lamb
Mary had a little
lamb
It's fleece was
white as snow

如果我添加了手动换行符,我希望它尊重这些换行符,因此,如果我在第一行之后两次按回车键,那么它也将在第一行下出现一个换行符.例如,如果我在自由格式框中写这篇文章,我希望它尊重每个新段落以及每个适当的换行符.我希望这可以解决所有问题.

If I added manual line breaks, I'd want it to respect those, so if I hit return twice after the first line, it'd have a line break under the first line as well. For example, if I wrote this post in the free form box, I'd want it to respect every new paragraph and every proper line break as well. I hope this clears things up.

推荐答案

我在我的代码中使用了类似的内容(假设\n是行分隔符):

I use something like this in my code (this assumes that \n is the line separator):

local function formatUpToX(s, x, indent)
  x = x or 79
  indent = indent or ""
  local t = {""}
  local function cleanse(s) return s:gsub("@x%d%d%d",""):gsub("@r","") end
  for prefix, word, suffix, newline in s:gmatch("([ \t]*)(%S*)([ \t]*)(\n?)") do
    if #(cleanse(t[#t])) + #prefix + #cleanse(word) > x and #t > 0 then
      table.insert(t, word..suffix) -- add new element
    else -- add to the last element
      t[#t] = t[#t]..prefix..word..suffix
    end
    if #newline > 0 then table.insert(t, "") end
  end
  return indent..table.concat(t, "\n"..indent)
end
print(formatUpToX(result, 20))
print(formatUpToX("@rHe eats @x123the bread", 8, "  "))

cleanse函数删除了需要包含在字符串中的所有标记,但是不需要计入限制.

The cleanse function removes any markup that needs to be included in the string, but doesn't need to count against the limit.

对于您拥有的示例,我得到以下输出(使用20作为第一个片段的限制,使用8作为第二个片段的限制):

For the example you have, I get the following output (using 20 as the limit for the first fragment and 8 for the second):

This is the result 
of three separate 
paragraphs.

As you can see, 
there is a line 
break between each 
paragraph.

So the result 
contains line 
breaks, or control 
characters.

  @rHe eats 
  @x123the 
  bread

这篇关于Lua:字符串中的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆