{"id":2177,"date":"2023-12-14T10:14:50","date_gmt":"2023-12-14T09:14:50","guid":{"rendered":"https:\/\/oopm.org\/?page_id=2177"},"modified":"2024-12-20T09:07:35","modified_gmt":"2024-12-20T08:07:35","slug":"51-2-a-text-filter","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=2177","title":{"rendered":"15.2 A text filter"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages2177&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages2177&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">Here we show an example in the domain of text processing. On the Macintosh, one may set an option that implies that two blanks in a text is replaced by a dot (<code>'.'<\/code>) and a blank. The coroutine <code>doubleSpaceToDot<\/code> below scans through a text string and makes these replacements.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>charFilter: <strong>obj<\/strong> \n   nextChar: <strong>var<\/strong> char\n   input: <strong>var<\/strong> String \n   i: <strong>var<\/strong> integer \n   next -> c: <strong>var<\/strong> char:\n      i := i + 1\n      c := input.get&#91;i]\n   send(ch: <strong>var<\/strong> char):\n      nextChar := ch\n   doubleSpaceToDot: <strong>obj<\/strong> \n      ch: <strong>var<\/strong> char\n      doubleSpaceToDot.suspend\n      cycle\n         ch := next\n         if (ch = ' ') :then\n            ch := next\n            if (ch = ' ') :then\n               send('.')\n               doubleSpaceToDot.suspend\n               send(' ')\n               doubleSpaceToDot.suspend\n            :else\n               send(' ')\n               doubleSpaceToDot.Suspend\n               send(ch)\n               doubleSpaceToDot.Suspend\n         :else\n            send(ch)\n            doubleSpaceToDot.Suspend\n   input := \"Hello world  What a nice day!\\n\"\n   loop: <strong>do<\/strong>\n      cycle \n         doubleSpaceToDot.call\n         put(nextChar)\n         if (nextChar = ascii.newline) :then\n            leave(loop)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The central element of this program is the coroutine object <code>doubleSpaceToDot<\/code>. When the object is generated, it starts by executing a <code>suspend<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The object <code>charFilter<\/code> has a local String <code>input<\/code>, which holds the text string to be filtered. For the purpose of the example, we assign it the <code>String<\/code> <code>\"Hello world  What a nice day!\\n\"<\/code>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>charFilter<\/code> then starts executing a cycle and the first statement in the <code>cycle<\/code> is a call of <code>doubleSpaceToDot<\/code>. This implies that <code>doubleSpaceToDot<\/code> is resumed and executes its <code>cycle<\/code>-statement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It assigns the next char in input to <code>ch<\/code> by executing <code>ch := next<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If <code>ch = ' '<\/code>,  it reads the next char and checks if it is also a blank. If blank it sends a dot (<code>'.'<\/code>) and suspends executions, and when resumed it sends a blank. If the next char is not a blank, it sends a blank one at a time and suspends in between.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>else<\/code>-part of the outermost <code>if<\/code>-statement, is executed If ch is not a blank and here it just sends ch and suspends execution.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">A simple version using a method<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A coroutine object may invoke methods and a method may suspend the coroutine object. In the next example we use this to make a simpler version of <code>doubleSpaceToDot<\/code>. Here the <code>send<\/code>-method is made a local method of <code>doubleSpaceToDot<\/code> and <code>send<\/code> executes the <code>suspend<\/code>, suspending the enclosing <code>doubleSpaceToDot<\/code> coroutine:<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>   doubleSpaceToDot: <strong>obj<\/strong> \n     ch: <strong>var<\/strong> char\n     send(ch: <strong>var<\/strong> char):\n        c := ch\n        doubleSpaceToDot.suspend\t\n     doubleSpaceToDot.suspend\n     cycle\n        ch := next\n        if (ch = ' ') :then\n           ch := next\n           if (ch = ' ') :then\n              send('.')\n              send(' ')\n           :else\n              send(' ')\n              send(ch)\n        :else\n           send(ch)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>send<\/code> executes a <code>suspend<\/code>, we have a situation where <code>doubleSpaceToDot<\/code> has invoked <code>send<\/code> after having read the <code>'o'<\/code> in <code>\"Hello\"<\/code> and thus <code>i = 5<\/code>. In the figure below, the left-side (A) shows the situation before suspend and the middle part (B) shows the situation after suspend.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"714\" height=\"336\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2023\/12\/CharFilter.png\" alt=\"\" class=\"wp-image-2505\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2023\/12\/CharFilter.png 714w, https:\/\/oopm.org\/wp-content\/uploads\/2023\/12\/CharFilter-300x141.png 300w\" sizes=\"(max-width: 714px) 100vw, 714px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The figure shows the activation stack. (A) shows that <code>charFilter<\/code> has called <code>doubleSpaceToDot<\/code>, which has called <code>send<\/code>. <code>Send<\/code> is currently the active object executing statements. Note that the elements<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\"> <\/mark>of the activation stack may be objects as well as method objects. Here the bottom element is an object and the other elements are method objects. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>send<\/code> executes <code>suspend<\/code>, the situation is shown at (B). <code>CharFilter.suspend<\/code> has the effect that control is returned to after <code>charFilter<\/code> has called <code>doubleSpaceToDot<\/code>. The arrow from <code>doubleSpaceToDot<\/code> illustrates that it is suspended and that <code>send<\/code> is at the top of its activation stack.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When <code>charFilter<\/code> makes another call to <code>doubleSpaceToDot<\/code>, the situation is as in the (C)-part, but with <code>ch = ' '<\/code> and <code>i = 6<\/code>. As can be seen, <code>doubleSpaceToDot<\/code> now again points to the calling <code>charFilter<\/code>-object.<\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=2177\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>Here we show an example in the domain of text processing. On the Macintosh, one may set an option that implies that two blanks in a text is replaced by a dot (&#8216;.&#8217;) and a blank. The coroutine doubleSpaceToDot below scans through a text string and makes these replacements. The central element of this program [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1961,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2177","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2177"}],"version-history":[{"count":31,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2177\/revisions"}],"predecessor-version":[{"id":10685,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/2177\/revisions\/10685"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1961"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}