Skip to content

Replace questionable characters in filenames #6

@hadley

Description

@hadley
library(gsubfn)
library(functional)

##' Substitutions of questionable characters with a hacker-joke to
##' boot.
substitutions=c(
  `!`='bang',
  `"`='quote',
  `#`='hash',
  `$`='cash',                           # sigil
  `%`='grapes',
  `&`='and',
  `'`='single-quote',
  `(`='open-paren',
  `)`='close-paren',
  `*`='star',
  `+`='plus',
  `,`='comma',
  `-`='dash',
  `.`='dot',
  `/`='slash',
  `:`='colon',
  `;`='semi-colon',
  `<`='less-than',
  `=`='equals',
  `>`='greater-than',
  `?`='p',
  `@`='asperand',
  `[`='open-brace',
  `\\`='backslash',
  `]`='close-brace',
  `^`='hat',
  `_`='sub',
  '`'='backtick',                       # let's add another ` to
                                        # rectify syntax highlighting
                                        # in emacs; thanks.
  `{`='open-curly',
  `|`='pipe',
  `}`='close',
  `~`='not'
  )

##' \code{NULL} if empty-string.
##' @param string string to check
##' @return \code{NULL} or identity
nil_if_lambda <- function(string)
  if (nchar(string)) string else NULL

##' Translate file-system-questionable characters (i.e. punctuation
##' within ASCII).
##' @param filename the filename to translate
##' @return the translated filename
translate_questionable_characters <- function(filename)
  do.call(Curry(paste, collapse="-"),
          strapply(filename,
                   pattern='([[:punct:]]|)([^[:punct:]]*|)',
                   function(punctuation, letters)
                   c(substitutions[nil_if_lambda(punctuation)],
                     nil_if_lambda(letters))))

translate_questionable_characters('%||%')

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions