class Schizm::String

Extend the default String class.

Public Instance Methods

comment() click to toggle source

Format as a C-style comment.

# File lib/schizm/string.rb, line 145
def comment
  s = "/*"
  each_line do |line|
    s.concat " "
    s.concat line
    s.concat " *"
  end
  s.concat "/"
end
erase(*args) click to toggle source

Erase occurances of +Regexp.union(*args)+.

# File lib/schizm/string.rb, line 39
def erase *args
  gsub Regexp.union(*args), ""
end
erase!(*args) click to toggle source

Erase occurances of +Regexp.union(*args)+.

# File lib/schizm/string.rb, line 34
def erase! *args
  gsub! Regexp.union(*args), ""
end
expand_tab(stop) click to toggle source

Expand tabs, subject to tabstop stop.

# File lib/schizm/string.rb, line 128
def expand_tab stop
  gsub /([^\t\n]*)\t/u do
    $1 + " " * (stop - $1.size % stop)
  end
end
expand_tab!(stop) click to toggle source

Expand tabs, subject to tabstop stop.

# File lib/schizm/string.rb, line 121
def expand_tab! stop
  gsub! /([^\t\n]*)\t/u do
    $1 + " " * (stop - $1.size % stop)
  end
end
find_indent() click to toggle source

Distance from last newline to length. If there's no last newline, just length.

# File lib/schizm/string.rb, line 136
def find_indent
  if (offset = rindex /\n/u)
    length - offset - 1
  else
    length
  end
end
guardify() click to toggle source

Make suitable string for include guard.

# File lib/schizm/string.rb, line 61
def guardify
  strip.wordify.upcase
end
html() click to toggle source

Encode non-ASCII characters and '&', '<', and '>' as HTML entities.

# File lib/schizm/string.rb, line 73
def html
  gsub /[&<>\p{^ASCII}]/u do |char|
    '&#' + char.codepoints[0].to_s + ';'
  end
end
html!() click to toggle source

Encode non-ASCII characters and '&', '<', and '>' as HTML entities.

# File lib/schizm/string.rb, line 66
def html!
  gsub! /[&<>\p{^ASCII}]/u do |char|
    '&#' + char.codepoints[0].to_s + ';'
  end
end
slim() click to toggle source

Replace space sequences by a single ASCII space.

# File lib/schizm/string.rb, line 116
def slim
  gsub /\p{Space}+/u, " "
end
slim!() click to toggle source

Replace space sequences by a single ASCII space.

# File lib/schizm/string.rb, line 111
def slim!
  gsub! /\p{Space}+/u, " "
end
slugify() click to toggle source

Make suitable string for slug.

# File lib/schizm/string.rb, line 54
def slugify
  gsub(/^\p{^Word}+/u, "")
    .gsub(/\p{^Word}+$/u, "")
    .gsub(/\p{^Word}+/u, '-').downcase
end
trim() click to toggle source

Remove blanks preceding newlines, remove trailing spaces, remove newlines succeeding empty lines, and remove leading newlines.

# File lib/schizm/string.rb, line 104
def trim
  copy = String.new self
  copy.trim!
  copy
end
trim!() click to toggle source

Remove blanks preceding newlines, remove trailing spaces, remove newlines succeeding empty lines, and remove leading newlines.

# File lib/schizm/string.rb, line 92
def trim!
  gsub! /\p{Blank}+\n/u, "\n"
  gsub! /\p{Space}+\z/u, ""
  gsub! /\n\n+/u, "\n\n"
  while start_with? "\n"
    slice! 0
  end
end
trim_blank() click to toggle source

Remove blanks preceding newlines.

# File lib/schizm/string.rb, line 85
def trim_blank
  gsub /\p{Blank}+\n/u, "\n"
end
trim_blank!() click to toggle source

Remove blanks preceding newlines.

# File lib/schizm/string.rb, line 80
def trim_blank!
  gsub! /\p{Blank}+\n/u, "\n"
end
truncate(n) click to toggle source

For sentence-like strings. Truncate to n characters and replace the final broken word with an ellipsis.

# File lib/schizm/string.rb, line 178
def truncate n
  if size <= n
    return String.new self
  else
    return String.new(self[0..n])
        .gsub(/\p{Space}+/u, " ")
        .gsub(/\p{Space}+\p{Graph}+\z/u, "...")
  end
end
unescape(*chars) click to toggle source

Remove backslashes which escape characters chars.

# File lib/schizm/string.rb, line 156
def unescape *chars
  s = String.new
  prev_char_bslash = false
  each_char do |char|
    if prev_char_bslash
      prev_char_bslash = false
      unless chars.include? char
        s.concat '\\'
      end
    else
      if char == '\\'
        prev_char_bslash = true
        next
      end
    end
    s.concat char
  end
  s
end
wordify() click to toggle source

Replace sequences of +p{^Word}+ by an underscore.

# File lib/schizm/string.rb, line 49
def wordify
  gsub /\p{^Word}+/u, '_'
end
wordify!() click to toggle source

Replace sequences of +p{^Word}+ by an underscore.

# File lib/schizm/string.rb, line 44
def wordify!
  gsub! /\p{^Word}+/u, '_'
end