All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nextflow.util.Escape.groovy Maven / Gradle / Ivy

Go to download

A DSL modelled around the UNIX pipe concept, that simplifies writing parallel and scalable pipelines in a portable manner

There is a newer version: 24.08.0-edge
Show newest version
/*
 * Copyright 2013-2024, Seqera Labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nextflow.util

import java.nio.file.Path

import groovy.transform.CompileStatic
import nextflow.extension.FilesEx

/**
 * Escape helper class
 *
 * @author Paolo Di Tommaso 
 */
@CompileStatic
class Escape {

    final private static List SPECIAL_CHARS = ["'", '"', ' ', '(', ')', '\\', '!', '&', '|', '<', '>', '`', ':', ';']

    final private static List VAR_CHARS = ['$']

    final private static List WILDCARDS = ["*", "?", "{", "}", "[", "]", "'", '"', ' ', '(', ')', '\\', '!', '&', '|', '<', '>', '`', ':']

    private static String replace(List special, String str, boolean doNotEscapeComplement=false) {
        def copy = new StringBuilder(str.size() +10)
        for( int i=0; i0 && str[i-1]=='[' && (i==1 || str[i-2]!='\\') && str.substring(i).contains(']'))
                if( !isComplement )
                    copy.append('\\')
            }
            copy.append(str[i])
        }
        return copy.toString()
    }

    static String wildcards(String str) {
        replace(WILDCARDS, str)
    }

    static String path(String val) {
        replace(SPECIAL_CHARS, val, true)
    }

    static String path(Path val) {
        path(val.toString())
    }

    static String path(File val) {
        path(val.toString())
    }

    static String path(GString val) {
        path(val.toString())
    }

    static String cli(String[] args) {
        args.collect { cli(it) }.join(' ')
    }

    static String cli(String arg) {
        if( arg.contains("'") )
            return wildcards(arg)
        def x = wildcards(arg)
        x == arg ? arg : "'" + arg + "'"
    }

    static String uriPath(Path file) {
        final scheme = FilesEx.getScheme(file)
        if( scheme == 'file' )
            return path(file)

        final uri = FilesEx.toUriString(file)
        final prefix = "$scheme://"
        assert uri.startsWith(prefix)
        def loc = uri.substring(prefix.length())
        while( loc.size()>1 && loc.endsWith('/') )
            loc = loc.substring(0,loc.length()-1)
        return prefix + path(loc)
    }

    static String blanks(String str) {
        str
                .replaceAll('\n',/\\n/)
                .replaceAll('\t',/\\t/)
                .replaceAll('\r',/\\r/)
                .replaceAll('\f',/\\f/)

    }

    static String variable(String val) {
        replace(VAR_CHARS, val, false)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy