Coder Social home page Coder Social logo

Comments (3)

nwg-piotr avatar nwg-piotr commented on August 19, 2024 1

I'll get back to it soon.

from nwg-drawer.

Figuera avatar Figuera commented on August 19, 2024

I wrote two functions to correct the behavior:

func parseString(s string) []string {
	var r = regexp.MustCompile(`(".*"|(\\\s|[^\s])+)`)
	var res = r.FindAllString(s, -1)
	return res
}

func parseString2(command string) []string {
	var elements []string
	var confirmed []string

	if !strings.Contains(command, "\"") {
		elements = append(elements, strings.Split(command, " ")...)
	} else {
		for strings.Contains(command, "\"") {
			quoteSplit := strings.SplitN(command, "\"", 2)
			elements = append(elements, strings.Split(quoteSplit[0], " ")...)
			quoteSplit = strings.SplitN(quoteSplit[1], "\"", 2)
			elements = append(elements, quoteSplit[0])
			command = quoteSplit[1]
		}
	}

	for i := 0; i < len(elements); i++ {
		if elements[i] != "" { // Ignore empty arguments
			element := elements[i]
			// If the argument end with "\ " then merge with next argument
			for element[len(element)-1] == '\\' {
				element = strings.Replace(element, "\\", "", 1)
				element = element + " " + elements[i+1]
				i++
			}
			confirmed = append(confirmed, element)
		}
	}

	return confirmed
}

Both seem to work fine in my testing but checking edge cases may be necessary, I don't like the second one as it is a little convoluted but running benchmarks proved that it is much faster than the regex option.

from nwg-drawer.

RiedleroD avatar RiedleroD commented on August 19, 2024

I also encountered this issue in nwg-menu. This seems important!

from nwg-drawer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.