Sha256: 833178a9f242c98f77069acac864b2cab87ad40e52a026c1cf0576d669732536

Contents?: true

Size: 1.65 KB

Versions: 12

Compression:

Stored size: 1.65 KB

Contents

package org.cx4a.rsense.util;

import java.util.List;
import java.util.ArrayList;

public class StringUtil {
    private StringUtil() {}

    public static String[] shellwords(String command) {
        List<String> result = new ArrayList<String>();
        char[] cs = command.toCharArray();
        int len = cs.length;
        StringBuilder sb = null;
        boolean escape = false;
        char quote = 0;
        for (int i = 0; i < len; ) {
            char c = cs[i++];

            // Skip whitespaces
            if (!escape && quote == 0) {
                while (i < len && Character.isWhitespace(c)) {
                    if (sb != null) {
                        result.add(sb.toString());
                        sb = null;
                    }
                    c = cs[i++];
                }
                if (i > len) {
                    break;
                }
            }

            // New field
            if (sb == null) {
                sb = new StringBuilder();
            }

            // Append character
            if (escape) {
                sb.append(c);
                escape = false;
            } else if (c == quote) {
                quote = 0;
            } else {
                switch (c) {
                case '"': case '\'':
                    quote = c;
                    break;
                case '\\':
                    escape = true;
                    break;
                default:
                    sb.append(c);
                    break;
                }
            }
        }
        if (sb != null) {
            result.add(sb.toString());
        }
        return result.toArray(new String[0]);
    }
}

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rsense-core-0.6.6 src/org/cx4a/rsense/util/StringUtil.java
rsense-core-0.6.5 src/org/cx4a/rsense/util/StringUtil.java
rsense-core-0.6.4 src/org/cx4a/rsense/util/StringUtil.java
rsense-core-0.6.2 src/org/cx4a/rsense/util/StringUtil.java
rsense-core-0.6.1 src/org/cx4a/rsense/util/StringUtil.java
rsense-core-0.6.0 src/org/cx4a/rsense/util/StringUtil.java
rsense-core-0.5.9 src/org/cx4a/rsense/util/StringUtil.java
rsense-core-0.5.8 src/org/cx4a/rsense/util/StringUtil.java
rsense-core-0.5.6 src/org/cx4a/rsense/util/StringUtil.java
rsense-core-0.5.2 src/org/cx4a/rsense/util/StringUtil.java
rsense-core-0.5.1 src/org/cx4a/rsense/util/StringUtil.java
rsense-core-0.5.0 src/org/cx4a/rsense/util/StringUtil.java