{"id":20397,"date":"2018-02-01T14:34:35","date_gmt":"2018-02-01T19:34:35","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=20397"},"modified":"2019-01-31T08:31:41","modified_gmt":"2019-01-31T13:31:41","slug":"c-string-functions-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/02\/01\/c-string-functions-in-swift\/","title":{"rendered":"C String Functions in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/www.alwaysrightinstitute.com\/swift-cstr\/\">Helge He&szlig;<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.alwaysrightinstitute.com\/swift-cstr\/\">\n<p>The pointer you get back points into a buffer, which doesn&rsquo;t exist anymore. That is why you need to be super careful when accessing C APIs.<\/p>\n<p>[&#8230;]<\/p>\n<p>When using C API with Swift Strings (be it a simple <code class=\"highlighter-rouge\">puts<\/code> or maybe libxml2),\nbe aware that such calls are <em>really<\/em> expensive (a malloc+free <em>per<\/em> call).\nIf you want to do this a lot, you may want to convert Strings to\nUTF-8 unsafe buffers very early on, and use those.<\/p>\n<p>Is this Swift behaviour reasonable?\nFor a high level language I would say yes.\nYet Swift also claims to be useful for system and server programming,\nand in such scenarios it is really hard to access standard Swift types\nin a performance sensible way (do zero copy, avoid allocs, etc).<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Helge He&szlig;: The pointer you get back points into a buffer, which doesn&rsquo;t exist anymore. That is why you need to be super careful when accessing C APIs. [&#8230;] When using C API with Swift Strings (be it a simple puts or maybe libxml2), be aware that such calls are really expensive (a malloc+free per [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2019-01-31T13:31:43Z","apple_news_api_id":"9f39f9c4-92f3-4f30-9ceb-3bfd00afdcb5","apple_news_api_modified_at":"2019-01-31T13:31:43Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/Anzn5xJLzTzCc6zv9AK_ctQ","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[45,571,54,71,901],"class_list":["post-20397","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-c","tag-memory-management","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=20397"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20397\/revisions"}],"predecessor-version":[{"id":24125,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/20397\/revisions\/24125"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=20397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=20397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=20397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}