{"id":7594,"date":"2018-11-05T15:21:29","date_gmt":"2018-11-05T20:21:29","guid":{"rendered":"http:\/\/irreal.org\/blog\/?p=7594"},"modified":"2018-11-05T15:21:29","modified_gmt":"2018-11-05T20:21:29","slug":"implementing-strings","status":"publish","type":"post","link":"https:\/\/irreal.org\/blog\/?p=7594","title":{"rendered":"Implementing Strings"},"content":{"rendered":"<p>\nAndreas Zwinkau has a very nice article on <a href=\"http:\/\/beza1e1.tuxen.de\/strings.html\">strings and how to implement them<\/a>. If you&rsquo;re a C programmer, your first reaction might be, &ldquo;Meh. They&rsquo;re an array of bytes. What&rsquo;s to implement?&rdquo; If you&rsquo;re, say, a Python programmer, you may not have thought about the question because, after all, they&rsquo;re a built-in primitive type.\n<\/p>\n<p>\nBut there are interesting questions to be asked, even for the C programmer. As a single example, it used to be that strings were made up of a series of characters, each a single byte. In these days of Unicode, that&rsquo;s not true even for the most parochial of middle American programmer with no intention of having users outside his town, let alone his country.\n<\/p>\n<p>\nThe first interesting question is string length. A simple byte count is no longer adequate when a character can be 1, 2, 3, or 4 bytes as they can in UTF-8. On the one hand, you need a byte count for low level operations such as allocating space for the string or copying one string to another. But very often, it&rsquo;s the character count that the application programmer is interested in so you need to record both or have any easy way of calculating them.\n<\/p>\n<p>\nSome programming language purists have railed for years about the traditional C method of marking the end of a string with a NULL byte and calculating string length by simply scanning down the string looking for the NULL and counting the bytes as you go. Yes, it&rsquo;s computationally expensive and yes, it can be a source of subtle bugs but it never bothered me or\u2014I&rsquo;d wager\u2014any competent C programmer. Regardless, it&rsquo;s clearly not adequate in a Unicode world so other implementations are needed.\n<\/p>\n<p>\nA large part of Zwinkau&rsquo;s post is concerned with this question but there are other interesting questions as well. If you&rsquo;re a programmer, you should definitely take a look at the article. It doesn&rsquo;t reveal any earth shattering, new, magical methods but it does show how to solve some of the problems involving strings that few of us have to worry about.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Andreas Zwinkau has a very nice article on strings and how to implement them. If you&rsquo;re a C programmer, your first reaction might be, &ldquo;Meh. They&rsquo;re an array of bytes. What&rsquo;s to implement?&rdquo; If you&rsquo;re, say, a Python programmer, you &hellip; <a href=\"https:\/\/irreal.org\/blog\/?p=7594\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[37],"class_list":["post-7594","post","type-post","status-publish","format-standard","hentry","category-programming","tag-tech"],"_links":{"self":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7594","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7594"}],"version-history":[{"count":1,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7594\/revisions"}],"predecessor-version":[{"id":7595,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7594\/revisions\/7595"}],"wp:attachment":[{"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/irreal.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}