Re: [swift-users] proper syntax for inout array handling?
On 2016-06-09 21:37, Joe Groff wrote: On Jun 9, 2016, at 8:39 PM, Saagar Jha via swift-userswrote: Nevermind, I lied. Swift does allow direct pointer arithmetic: import Foundation var source = [UInt8](repeating: 0x1f, count: 32) var destination = [UInt8](repeating: 0, count: 64) memcpy(, source, 32) // the C function memcpy( + 3, source, 13) // the + operator works to offset Arrays can indeed be used as pointer parameters, but the second one only works by accident. The pointer bridging has the same semantics as an 'inout' parameter, so the pointer is only valid for the duration of the immediate call, and since operators in Swift are also function calls, the pointer expires after the '+' operation. If you're doing anything with an array other than passing it off to a single C function, you should use withUnsafeMutableBufferPointer instead: destination.withUnsafeMutableBufferPointer { p in memcpy(p.baseAddress, source, 32) memcpy(p.baseAddress + 3, source, 13) } In addition to not having undefined behavior, this will also probably be faster, since it'll only need to pin the array for pointer access once instead of twice. -Joe Thanks for the good answers, both Saager and Joe. I like the way you both developed solutions and then improved them, and for good reasons. I do have a situation in the ugly C code I am porting to Swift where a pointer gets passed down to a second function, so Joe's solution looks like the right approach. Thanks again. -- Ken Burgett Principal Software Engineer Email: k...@iotone.io Office: 530.693.4449 Mobile: 831.332.6846 URL: www.iotone.co ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
[swift-users] Looking for a Swift3 JSON parser
I am looking for a parser for JSON is compatible with Swift 3 on Linux. SwiftyJSON looks interesting, but it is Swift 2.2 compatible, AFAIK. Any URL will be appreciated, and a Swift3 package would bring joy. -- Ken Burgett Principal Software Engineer Email: k...@iotone.io Office: 530.693.4449 Mobile: 831.332.6846 URL: www.iotone.co ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
Re: [swift-users] Simple text file I/O with Swift 3 (Quinn "The Eskimo!")
On 2016-05-30 10:00, swift-users-requ...@swift.org wrote: Send swift-users mailing list submissions to swift-users@swift.org To subscribe or unsubscribe via the World Wide Web, visit https://lists.swift.org/mailman/listinfo/swift-users or, via email, send a message with subject or body 'help' to swift-users-requ...@swift.org You can reach the person managing the list at swift-users-ow...@swift.org When replying, please edit your Subject line so it is more specific than "Re: Contents of swift-users digest..." Today's Topics: 1. Re: Simple text file I/O with Swift 3 (Quinn "The Eskimo!") -- Message: 1 Date: Mon, 30 May 2016 09:11:41 +0100 From: "Quinn \"The Eskimo!\"" <eski...@apple.com> To: Swift Users List <swift-users@swift.org> Subject: Re: [swift-users] Simple text file I/O with Swift 3 Message-ID: <d94a5001-be92-44fb-adb8-ce0d68e62...@apple.com> Content-Type: text/plain; charset=us-ascii On 28 May 2016, at 19:05, Ken Burgett via swift-users <swift-users@swift.org> wrote: print(buf) The trick here is to replace the above line with: print(String(validatingUTF8: buf)) `fgets` sets up `buf` to hold a C string, so you have to convert it to a Swift string. How do you do this depends on the encoding of the bytes. If you expect the C string to be UTF-8, then `String(validatingUTF8:)` is the way to go. IMPORTANT: This conversion can fail, which is why the above will print a bunch of optional strings, and you will have to decide what to your program should do when it does. * * * btw This question came up recently. See the thread for other suggestions about how to handle it. <http://article.gmane.org/gmane.comp.lang.swift.user/1943> Share and Enjoy -- Quinn "The Eskimo!" <http://www.apple.com/developer/> Apple Developer Relations, Developer Technical Support, Core OS/Hardware -- ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users End of swift-users Digest, Vol 6, Issue 28 ** Hi Quinn, The link you referenced above <http://article.gmane.org/gmane.comp.lang.swift.user/1943> is all about Swift 2.2. I had already found that one on StackOverflow and gave it a try, which caused me to generate the string of queries, when it all fell apart under Swift 3. -- Ken Burgett Principal Software Engineer Email: k...@iotone.io Office: 530.693.4449 Mobile: 831.332.6846 URL: www.iotone.co ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
Re: [swift-users] swift-users Digest, Vol 6, Issue 28
On 2016-05-30 10:00, swift-users-requ...@swift.org wrote: Send swift-users mailing list submissions to swift-users@swift.org To subscribe or unsubscribe via the World Wide Web, visit https://lists.swift.org/mailman/listinfo/swift-users or, via email, send a message with subject or body 'help' to swift-users-requ...@swift.org You can reach the person managing the list at swift-users-ow...@swift.org When replying, please edit your Subject line so it is more specific than "Re: Contents of swift-users digest..." Today's Topics: 1. Re: Simple text file I/O with Swift 3 (Quinn "The Eskimo!") -- Message: 1 Date: Mon, 30 May 2016 09:11:41 +0100 From: "Quinn \"The Eskimo!\"" <eski...@apple.com> To: Swift Users List <swift-users@swift.org> Subject: Re: [swift-users] Simple text file I/O with Swift 3 Message-ID: <d94a5001-be92-44fb-adb8-ce0d68e62...@apple.com> Content-Type: text/plain; charset=us-ascii On 28 May 2016, at 19:05, Ken Burgett via swift-users <swift-users@swift.org> wrote: print(buf) The trick here is to replace the above line with: print(String(validatingUTF8: buf)) `fgets` sets up `buf` to hold a C string, so you have to convert it to a Swift string. How do you do this depends on the encoding of the bytes. If you expect the C string to be UTF-8, then `String(validatingUTF8:)` is the way to go. IMPORTANT: This conversion can fail, which is why the above will print a bunch of optional strings, and you will have to decide what to your program should do when it does. * * * btw This question came up recently. See the thread for other suggestions about how to handle it. <http://article.gmane.org/gmane.comp.lang.swift.user/1943> Share and Enjoy -- Quinn "The Eskimo!" <http://www.apple.com/developer/> Apple Developer Relations, Developer Technical Support, Core OS/Hardware -- ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users End of swift-users Digest, Vol 6, Issue 28 ** Hi Quinn, Thanks for the tip on print(String(validatingUTF8: buf)), that does reproduce my input text, line for line, EXCEPT for wrapping every line in "Optional(line-of-text-with-terminator)", for example "Optional("import Glibc\n")". So, how does wrapping a line of UTF8 text in another character string "Optional()" help me print the text? Is Optional() some kind of function? If so, how is it intended to be used? -- Ken Burgett Principal Software Engineer Email: k...@iotone.io Office: 530.693.4449 Mobile: 831.332.6846 URL: www.iotone.co ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
[swift-users] Simple text file I/O with Swift 3
Hi all, I am trying to understand how Swift 3 File I/O works in a linux environment. I put together a trivial test program using what I can glean from the few examples I can find. See below. === import Glibc import Foundation let filename = Process.arguments[1] let file_handle = fopen (filename, "r") let BUFSIZE = 1024 var buf = [CChar](repeating:CChar(0), count:BUFSIZE) while fgets(, Int32(BUFSIZE), file_handle) != nil { print(buf) } exit(0) === The program runs, but produces a stream of integer arrays, each of 1024 bytes. I can see what appears to be ASCII character values in the sample text, plus a lot of trailing zeros. I suspect I haven't mapped the bytes in buf to Strings, so how should that be done? -- Ken Burgett Principal Software Engineer Email: k...@iotone.io Office: 530.693.4449 Mobile: 831.332.6846 URL: www.iotone.coimport Glibc import Foundation // // 'main program' // // command: "blake2b filename [key]", // guard Process.arguments.count == 2 else { print("Usage: blake2b FILENAME") exit(-1) } let filename = Process.arguments[1] let file_handle = fopen (filename, "r") let BUFSIZE = 1024 var buf = [CChar](repeating:CChar(0), count:BUFSIZE) while fgets(, Int32(BUFSIZE), file_handle) != nil { print(buf) } exit(0) ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
Re: [swift-users] Printing large hexadecimal values
On 2016-05-25 12:00, Jens Alfke wrote: On May 25, 2016, at 11:11 AM, Ken Burgettwrote: the "%llx" field is not getting interpreted... You have to import Foundation to bring in the String.init(format:…) method, which is bridged from Foundation's NSString class. (This is a temporary inconvenience until the Swift standard library is complete.) —Jens Hi Jens, You are correct, the "%llx" works for UInt64, while "%16x" does not. "%llX" also works, producing an uppercase string. Should this be reported as a bug? -- Ken Burgett Principal Software Engineer Email: k...@iotone.io Office: 530.693.4449 Mobile: 831.332.6846 URL: www.iotone.co ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
[swift-users] Printing large hexadecimal values
I really shouldn't have to ask this question for a variety of other languages, but print format conversion in Swift a black box to me. I wish to print a 64-bit unsigned integer as 8 hexadecimal digits, and I can't find any documentation on this. Please advise. -- Ken Burgett Principal Software Engineer Email: k...@iotone.io Office: 530.693.4449 Mobile: 831.332.6846 URL: www.iotone.co ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
[swift-users] error: NoSources("/home/kenb/.../Cpp_Swift/Sources/cwrapper")
I tried build a system module, following the cpp and swift variant mentioned here (http://ankit.im/swift/2016/05/21/creating-objc-cpp-packages-with-swift-package-manager/). I hope the author will answer in detail, but I would like to know what produces the 'error: NoSources(..)' error message? -- Ken Burgett Principal Software Engineer Email: k...@iotone.io Office: 530.693.4449 Mobile: 831.332.6846 URL: www.iotone.co ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
[swift-users] Need documentation on how to import C header into Swift 3.0
Hi Joe, Can you point me to a good example of how to build a Swift 3.0 module that wraps some C code? I have searched, but keep finding Swift 2.2 documentation and it all seems to involve connecting to Objective-C, while my interests are accessing C code on a Linux platform, using the latest Ubuntu Linux 14.04 version of the Swift 3.0 toolchain. Thanks in advance for your help. -- Ken Burgett Principal Software Engineer Email: k...@iotone.io Office: 530.693.4449 Mobile: 831.332.6846 URL: www.iotone.co ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users
Re: [swift-users] Mapping C semantics to Swift
On 2016-05-20 08:55, Jens Alfke wrote: On May 20, 2016, at 7:33 AM, Ken Burgett via swift-users <swift-users@swift.org> wrote: the required C struct morphs into a Swift class, with a member function 'as_C_byte_array' which answers an array of the required form. Internally, this function will shift and mask class attributes in order to build the necessary result. It would be nice to see Swift acquire structured-data I/O APIs as found in languages like Ruby and Python — these take a printf-like format string and parameters, but read and write values in binary. The format syntax offers modifiers to specify things like byte order, string encoding, etc. This is so much more reliable and readable than doing the encoding by hand. —Jens Hi Jens, I understand the value of strong typing from a runtime point of view, but I really miss the quickness and simplicity of writing a class in Ruby, where duck-typing is king. -- Ken Burgett Principal Software Engineer Email: k...@iotone.io Office: 530.693.4449 Mobile: 831.332.6846 URL: www.iotone.co ___ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users