gpt4 book ai didi

string - 字符串的 pretty-print (确保自动换行符保持在给定的打印边距内)

转载 作者:行者123 更新时间:2023-12-02 03:00:39 25 4
gpt4 key购买 nike

对于与字符串相关的所有内容,我尝试坚持 80 个字符的打印边距,尤其是关于定制消息(警告、错误等)

现在,我一直想知道是否有一种简单的方法来确保自动确保/强制执行某些打印边距限制,例如如果给定的原子字符串超出了所选的打印边距,则它会自动分解为字符串向量。我用谷歌搜索了一下,但没有真正找到任何符合我需求的东西。

问题

是否有人想出了一个很好的“ pretty-print ”功能,我可以进一步构建它?

<小时/>

到目前为止,这是我自己想到的:

函数定义

makePretty <- function(
string,
print.margin=80,
...
){
out <- string
if (nchar(string) > print.margin) {
times <- ceiling(nchar(string)/print.margin)
breaks <- rep(print.margin+1, times)
breaks <- cumsum(breaks)
string.spl <- unlist(strsplit(string, split=" "))
seps <- str_locate_all(string, " ")[[1]][,"start"]
queue <- NA

envir <- environment()
out <- unlist(sapply(1:length(breaks), function(ii) {
idx.next <- breaks[ii]
if (ii < length(breaks)) {
idx <- which(seps <= idx.next)
chunk <- string.spl[idx]
envir$string.spl <- envir$string.spl[-idx]
envir$seps <- envir$seps[-idx]
} else {
chunk <- string.spl
}
chunk <- paste(chunk, collapse=" ")
# Chunk could exceed print margin in case the right hand neighbor
# wasn't a blank >> check again
if (nchar(chunk) > print.margin) {
chunk <- makePretty(string=chunk, print.margin=print.margin)
envir$queue <- chunk[length(chunk)]
chunk <- chunk[-length(chunk)]
} else {
if (!is.na(envir$queue)) {
# Prepend chunk with queued chunk
chunk <- paste(envir$queue, chunk, sep=" ")
# Reset queue
envir$queue <- NA
}
}
# /
out <- chunk
return(out)
}))
}
return(out)
}

函数应用

string <- "This is just an example of a very long character string that exceeds the default print margin of 80 and therefore needs some pretty printing. In fact it's so long that it needs to be broken down into three parts."
> makePretty(string=string)
[1] "This is just an example of a very long character string that exceeds the default"
[2] "print margin of 80 and therefore needs some pretty printing. In fact it's so"
[3] "long that it needs to be broken down into three parts."

> string <- "This is just an example of a very long character string that exceeds a certain print margin and therefore needs some pretty printing. In fact it's so long that it needs to be broken down into numerous parts."
> makePretty(string=string, print.margin=40)
[1] "This is just an example of a very long"
[2] "character string that exceeds a certain"
[3] "print margin and therefore needs some"
[4] "pretty printing. In fact it's so long"
[5] "that it needs to be broken down into numerous"
[6] "parts."

string <- "This is just an example of a very long character string that exceeds the default print margin of 80 and therefore needs some pretty printing. In fact it's so looooooooooooooooooooooooooooooooong that it needs to be broken down into four parts."
> makePretty(string=string)
[1] "This is just an example of a very long character string that exceeds the default"
[2] "print margin of 80 and therefore needs some pretty printing. In fact it's so"
[3] "looooooooooooooooooooooooooooooooong that it needs to be broken down into four"
[4] "parts."

string <- "This is just an example of a very long character string that exceeds the default print margin of 80 and therefore needs some pretty printing. In fact it's soooooooo looooooooooooooooooooooooooooooooong that it needs to be broken down into four parts."
> makePretty(string=string)
[1] "This is just an example of a very long character string that exceeds the default"
[2] "print margin of 80 and therefore needs some pretty printing. In fact it's"
[3] "soooooooo looooooooooooooooooooooooooooooooong that it needs to be broken down"
[4] "into four parts."

到目前为止,该方法仅依靠空格来确定哪些单词属于一起 - 这可能不涵盖其他“现实世界场景”,例如冒号、分号等。

最佳答案

基本 R 函数 strwrap 似乎完全符合您的描述:

strwrap(x, width=80)
[1] "This is just an example of a very long character string that exceeds the"
[2] "default print margin of 80 and therefore needs some pretty printing. In fact"
[3] "it's so long that it needs to be broken down into three parts."

strwrap(x, 40)
[1] "This is just an example of a very long" "character string that exceeds the"
[3] "default print margin of 80 and" "therefore needs some pretty printing."
[5] "In fact it's so long that it needs to" "be broken down into three parts."
<小时/>

您可以使用 paste 和参数 collapse="\n" 将这些片段组合成带有换行符的单个字符串:

cat(paste(strwrap(x, 40), collapse="\n"))
This is just an example of a very long
character string that exceeds the
default print margin of 80 and
therefore needs some pretty printing.
In fact it's so long that it needs to
be broken down into three parts.

关于string - 字符串的 pretty-print (确保自动换行符保持在给定的打印边距内),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12160803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com