File: strwrap.s

package info (click to toggle)
hmisc 5.2-5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,044 kB
  • sloc: asm: 28,907; f90: 590; ansic: 415; xml: 160; fortran: 75; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 2,615 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
if(!exists('strwrap')) {
  strwrap <- function (x, width = 0.9 * getOption("width"), indent = 0, exdent = 0,
                       prefix = "", simplify = TRUE) {
    indentString <- paste(rep.int(" ", indent), collapse = "")
    exdentString <- paste(rep.int(" ", exdent), collapse = "")
    y <- list()
    z <- lapply(strsplit(x, "\n[ \t\n]*\n"), strsplit, "[ \t\n]")
    for (i in seq(along = z)) {
      yi <- character(0)
      for (j in seq(along = z[[i]])) {
        words <- z[[i]][[j]]
        nc <- nchar(words, type = "w")
        if (any(is.na(nc))) {
          nc0 <- nchar(words)
          nc[is.na(nc)] <- nc0[is.na(nc)]
        }
        if (any(nc == 0)) {
          zLenInd <- which(nc == 0)
          zLenInd <- zLenInd[!(zLenInd %in% (grep("\\.$",
                                                  words) + 1))]
          if (length(zLenInd) > 0) {
            words <- words[-zLenInd]
            nc <- nc[-zLenInd]
          }
        }
        if (length(words) == 0) {
          yi <- c(yi, "", prefix)
          next
        }
        currentIndex <- 0
        lowerBlockIndex <- 1
        upperBlockIndex <- integer(0)
        lens <- cumsum(nc + 1)
        first <- TRUE
        maxLength <- width - nchar(prefix, type = "w") -
          indent
        while (length(lens) > 0) {
          k <- max(sum(lens <= maxLength), 1)
          if (first) {
            first <- FALSE
            maxLength <- maxLength + indent - exdent
          }
          currentIndex <- currentIndex + k
          if (nc[currentIndex] == 0)
            upperBlockIndex <- c(upperBlockIndex, currentIndex -
                                 1)
          else upperBlockIndex <- c(upperBlockIndex, currentIndex)
          if (length(lens) > k) {
            if (nc[currentIndex + 1] == 0) {
              currentIndex <- currentIndex + 1
              k <- k + 1
            }
            lowerBlockIndex <- c(lowerBlockIndex, currentIndex +
                                 1)
          }
          if (length(lens) > k)
            lens <- lens[-(1:k)] - lens[k]
          else lens <- NULL
        }
        nBlocks <- length(upperBlockIndex)
        s <- paste(prefix, c(indentString, rep.int(exdentString,
                                                   nBlocks - 1)), sep = "")
        for (k in (1:nBlocks)) s[k] <- paste(s[k], paste(words[lowerBlockIndex[k]:upperBlockIndex[k]],
                                                         collapse = " "), sep = "")
        yi <- c(yi, s, prefix)
      }
      y <- c(y, list(yi[-length(yi)]))
    }
    if (simplify)
      y <- unlist(y)
    y
  }
}