From 9abc06ae08ed7ffbd43187e9ead8994b07ebca53 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Sun, 1 Mar 2020 13:03:51 +0100 Subject: [PATCH] Add ingo#str#trcd() variant of ingo#str#trd() --- README.md | 1 + autoload/ingo/str.vim | 21 ++++++++++++++++++++- doc/ingo-library.txt | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 12912b2..b18aa6e 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,7 @@ HISTORY - Add optional a:Predicate argument to ingo#area#frompattern#Get() and ingo#text#frompattern#Get(). Additionally, the a:isOnlyFirstMatch and a:isUnique arguments are optional now, too. +- Add ingo#str#trcd() variant of ingo#str#trd(). ##### 1.040 31-Oct-2019 - Add ingo#str#TrimTrailing() variant of ingo#str#Trim(). diff --git a/autoload/ingo/str.vim b/autoload/ingo/str.vim index f5ed77f..2321153 100644 --- a/autoload/ingo/str.vim +++ b/autoload/ingo/str.vim @@ -5,7 +5,7 @@ " - ingo/regexp/virtcols.vim autoload script " - ingo/str/list.vim autoload script " -" Copyright: (C) 2013-2019 Ingo Karkat +" Copyright: (C) 2013-2020 Ingo Karkat " The VIM LICENSE applies to this script; see ':help copyright'. " " Maintainer: Ingo Karkat @@ -141,6 +141,25 @@ function! ingo#str#trd( src, fromstr ) return substitute(a:src, '\C' . ingo#regexp#collection#LiteralToRegexp(a:fromstr), '', 'g') endfunction +function! ingo#str#trcd( src, fromstr ) +"****************************************************************************** +"* PURPOSE: +" Keep only characters in a:fromstr in a copy of a:src. Like tr -cd, but the +" built-in tr() doesn't support this. +"* ASSUMPTIONS / PRECONDITIONS: +" None. +"* EFFECTS / POSTCONDITIONS: +" None. +"* INPUTS: +" a:src Source string. +" a:fromstr Characters that will each be kept in a:src. +"* RETURN VALUES: +" Copy of a:src that has all instances of the complement of the characters in +" a:fromstr removed. +"****************************************************************************** + return substitute(a:src, '\C[^' . ingo#regexp#collection#LiteralToRegexp(a:fromstr)[1:], '', 'g') +endfunction + function! ingo#str#Wrap( string, commonOrPrefix, ... ) abort "****************************************************************************** "* PURPOSE: diff --git a/doc/ingo-library.txt b/doc/ingo-library.txt index c053cff..8117bfe 100644 --- a/doc/ingo-library.txt +++ b/doc/ingo-library.txt @@ -174,6 +174,7 @@ HISTORY *ingo-library-history* - Add optional a:Predicate argument to ingo#area#frompattern#Get() and ingo#text#frompattern#Get(). Additionally, the a:isOnlyFirstMatch and a:isUnique arguments are optional now, too. +- Add ingo#str#trcd() variant of ingo#str#trd(). 1.040 31-Oct-2019 - Add ingo#str#TrimTrailing() variant of ingo#str#Trim().