Groovy: How can I include backslashes inside a string without escaping?

Wow, another gotcha with putting Windows files paths in slashy strings. Nice catch. The gotcha I’ve encountered before was including a trailing backslash on the path, e.g. /C:\path\/, which results in an unexpected char: 0xFFFF error. Anyways, for the sake of an answer, given that Windows paths are case insensitive, why not exploit it for … Read more

Slice a string containing Unicode chars

Possible solutions to codepoint slicing I know I can use the chars() iterator and manually walk through the desired substring, but is there a more concise way? If you know the exact byte indices, you can slice a string: let text = “Hello привет”; println!(“{}”, &text[2..10]); This prints “llo пр”. So the problem is to … Read more

How to convert String to UnsafePointer and length

You have to convert the string to UTF-8 data first let string = “foo bar” let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)! and then write it to the output stream let outputStream: NSOutputStream = … // the stream that you want to write to let bytesWritten = outputStream.write(UnsafePointer(data.bytes), maxLength: data.length) The UnsafePointer() cast is necessary because … Read more

grep without string

Either of the these will do: grep -v “def” input_file | grep “abc” or grep “abc” input_file | grep -v “def” The following will also preserve coloring if you only want to see the output on stdout: grep –color=always “abc” input_file | grep -v “def” The -v option (stands for “invert match”) tells grep to … Read more