{"id":706,"date":"2010-12-02T13:00:31","date_gmt":"2010-12-02T01:00:31","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=706"},"modified":"2010-12-02T13:02:23","modified_gmt":"2010-12-02T01:02:23","slug":"making-a-case-for-strings-the-sane-way","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/706\/","title":{"rendered":"Making a case for Strings, the sane way"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\"> &lt; 1<\/span> <span class=\"rt-label rt-postfix\">minute]<\/span><\/span><p><a href=\"http:\/\/delphi.fosdal.com\/\">Lars Fosdal<\/a> responded to <a href=\"https:\/\/www.deltics.co.nz\/blog\/?p=700\">my previous post<\/a> suggesting a way of implementing string support in a case-like construct (but not actually a case statement) <a href=\"http:\/\/delphi.fosdal.com\/2010\/12\/generic-case-for-strings.html\">using generics and anonymous methods<\/a>.<\/p>\n<p>All very clever, but way, way too complicated and &#8211; if you don&#8217;t mind me saying so &#8211; as ugly as sin into the bargain (imho &#8211; ymmv).<\/p>\n<p>For simple cases [sic], it is actually relatively straightforward to uses strings in a case statement.<br \/>\n<!--more--><\/p>\n<p>Just add this function to a convenient unit:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\r\n  interface\r\n\r\n    function StringIndex(const aString: String;\r\n                         const aCases: array of String;\r\n                         const aCaseSensitive: Boolean = TRUE): Integer;\r\n\r\n  implementation \r\n\r\n    function StringIndex(const aString: String;\r\n                         const aCases: array of String;\r\n                         const aCaseSensitive: Boolean): Integer;\r\n    begin\r\n      if aCaseSensitive then\r\n      begin\r\n        for result := 0 to Pred(Length(aCases)) do\r\n          if ANSISameText(aString, aCases&#x5B;result]) then\r\n            EXIT;\r\n      end\r\n      else\r\n      begin\r\n        for result := 0 to Pred(Length(aCases)) do\r\n          if ANSISameStr(aString, aCases&#x5B;result]) then\r\n            EXIT;\r\n      end;\r\n\r\n      result := -1;\r\n    end;\r\n<\/pre>\n<p>And now you can use this in an ordinary case statement:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n  case StringIndex(SomeString, &#x5B;SelectorA,\r\n                                SelectorB,\r\n                                SelectorC]) of\r\n    0: \/\/ for SelectorA\r\n    1: \/\/ for SelectorB\r\n    2: \/\/ for SelectorC\r\n  else\r\n    \/\/ Some other SomeString\r\n  end;\r\n<\/pre>\n<p>I added a case-sensitivity switch, but more sophisticated options could be provided such as partial matching.  Similarly, there could possibly be some optimisation of the string matching within the function (perhaps triggered by a number of cases above a certain threshold), but this is the bare bones of what you need.<\/p>\n<p>In fact, I am pretty sure I&#8217;ve seen something similar to this in the past, but I can&#8217;t put my finger on when or where.<\/p>\n<p>In any event, build on, improve, embellish to your hearts content, and all with perfectly clear, concise, Pascal-like syntax in <strong>all<\/strong> versions of Delphi.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\"> &lt; 1<\/span> <span class=\"rt-label rt-postfix\">minute]<\/span><\/span>Lars Fosdal responded to my previous post suggesting a way of implementing string support in a case-like construct (but not actually a case statement) using generics and anonymous methods. All very clever, but way, way too complicated and &#8211; if you don&#8217;t mind me saying so &#8211; as ugly as sin into the bargain (imho [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4],"tags":[125,13,127,128],"class_list":["post-706","post","type-post","status-publish","format-standard","hentry","category-delphi","tag-case","tag-language","tag-object-pascal","tag-strings"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-bo","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/706","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=706"}],"version-history":[{"count":4,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/706\/revisions"}],"predecessor-version":[{"id":710,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/706\/revisions\/710"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}