文字列の変換 -- translit2014年12月07日 12:12

コマンドラインの引数がとれるようになったので、引数を使ったプログラムを 紹介します。 文字列の置き換えプログラムtranslitです。

translitは、標準入力から読み込んだ文字をコマンドラインの引数の指示に従い変換します。たとえば、
translit x y
は、文字"x"を文字"y"に変換します。また、
translit xy yx
は、文字"x"を文字"y"に、文字"y"を文字"x"に変換します。さらには、
translit a-z A-Z
は、英小文字を英大文字に変換します。"a-z"は、"abcdefghijklmnopqrstuvwxyz"の略記です。
translit a-zA-Z a
とすれば、英文字列を文字"a"一文字に押しつぶします。また、
translit 0-9 n
とすれば、数字列を文字"n"一文字に押しつぶします。
translit 0-9
とすれば、数字列を削除することになります。もう少し拡張します。
translit !a-z -
とすれば、英小文字以外は、改行も含め、"-"に変換されます。

脱出文字"@"も使えます。"@n"とすれば改行を意味し、"@t"は、タブです。"@"自身は"@@"と表現できます。
translit @n N
とすれば、改行が文字"N"に変換され、
translit @t T
とすれば、タブが文字"T"に変換されます。

このように、translitは一種のフィルターです。

translitのメイン部分は下記の通り。

# translit.r4 -- map characters
      character getc
      character arg(MAXARR), c, from(MAXSET), to(MAXSET)
      integer getarg, length, makset, xindex
      integer allbut, collap, i, lastto

      allbut = NO
      if (getarg(1, arg, MAXARR) == NO)
          call error('usage: translit from to.')
      else if (arg(1) == NOT)
          allbut = YES
          if (makset(arg, 2, from, MAXSET) == NO)
              call error('from: too large.')
      else 
          allbut = NO
          if (makset(arg, 1, from, MAXSET) == NO)
              call error('from: too large.')

      if (getarg(2,arg,MAXARR) == NO)
          to(1) = EOS
      else if (makset(arg, 1, to, MAXSET) == NO)
          call error('to: too large.')

      lastto = length(to)
      if (length(from) > lastto | allbut == YES)
          collap = YES
      else
          collap = NO

      repeat {
          i = xindex(from, getc(c), allbut, lastto)
          if (collap == YES & i >= lastto & lastto > 0) { # collapse
              call putc(to(lastto))
              repeat
                  i = xindex(from, getc(c), allbut, lastto)
              until (i < lastto)
              }
          if (c == EOF) { 
              call putc(EOF)
              break
              }
          if (i > 0 & lastto > 0)       # translate
              call putc(to(i))
          else if (i == 0)              # copy
              call putc(c)
                                        # else delete
          }
      stop
      end

プログラムの前半は、コマンドラインの引数から、変換ルールの文字列を作成することに費やされています。 後半は、標準入力からEOFを読みとるまで、変換して標準出力へ書き出します。

Watcom Fortran77版は、下記の通り。

c translit.for -- map characters
      program translit
      integer*1 getc
      integer*1 arg(100), c, from(200), to(200) ! MAXARR(100) MAXSET(00)
      integer getarg, length, makset, xindex
      integer allbut, collap, i, lastto

      allbut = 0                        ! NO(0)
      if (getarg(1, arg, 100) .eq. 0) then ! MAXARR(100) NO(0)
          call error('usage: translit from to.')
      else if (arg(1) .eq. 33) then     ! NOT(33)
          allbut = 1                    ! YES(1)
          if (makset(arg, 2, from, 200) .eq. 0) then ! MAXSET(200) NO(0)
              call error('from: too large.')
          end if
      else 
          allbut = 0                    ! NO(0)
          if (makset(arg, 1, from, 200) .eq. 0) then ! MAXSET(200) NO(0)
              call error('from: too large.')
          end if
      end if

      if (getarg(2, arg, 100) .eq. 0) then ! MAXARR(100)
          to(1) = -2                    ! EOS
      else if (makset(arg, 1, to, 200) .eq. 0) then ! MAXSET(200) NO(0)
          call error('to: too large.')
      end if

      lastto = length(to)
      if (length(from) .gt. lastto .or. allbut .eq. 1) then ! YES(1)
          collap = 1                    ! YES(1)
      else
          collap = 0                    ! NO(0)
      end if

      loop
          i = xindex(from, getc(c), allbut, lastto)
          if (collap .eq. 1
     1        .and. i .ge. lastto .and. lastto .gt. 0) then ! collapse
              call putc(to(lastto))
              loop
                  i = xindex(from, getc(c), allbut, lastto)
              until (i .lt. lastto)
          end if
          if (c .eq. -1) then           ! EOF(-1)
              call putc(-1)             ! EOF(-1)
              exit
          end if
          if (i .gt. 0 .and. lastto .gt. 0) then  ! translate
              call putc(to(i))
          else if (i .eq. 0) then       ! copy
              call putc(c)
          end if                        ! else delete
      end loop
      stop
      end

次回からは、translitの下請けルーチンを紹介します。

コメント

_ Pof Mobile ― 2015年10月18日 05:29

Greetings from California! I'm bored at work so I decided to browse your blog on my iphone during lunch break. I enjoy the knowledge you present here and can't wait to take a look when I get home. I'm surprised at how quick your blog loaded on my cell phone .. I'm not even using WIFI, just 3G .. Anyhow, wonderful site!

_ quest protein bars review ― 2015年10月19日 00:57

Valuable information. Fortunate me I found your website by chance, and I am shocked why this twist of fate did not came about earlier! I bookmarked it.

_ quest bar reviews ― 2015年10月19日 11:23

I'm really impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Anyway keep up the excellent quality writing, it's rare to see a great blog like this one these days.

_ quest-nutrition.com ― 2015年10月27日 00:45

Remarkable! Its actually amazing piece of writing, I have got much clear idea concerning from this post.

_ plenty of fish dating site of free dating ― 2015年11月06日 05:29

Very good information. Lucky me I recently found your blog by chance (stumbleupon).
I have saved as a favorite for later!

_ plenty of fish dating site of free dating ― 2015年11月06日 19:12

Howdy I am so delighted I found your web site, I really found you by error, while I was searching on Askjeeve for
something else, Regardless I am here now and would just like to say thank you for a marvelous
post and a all round thrilling blog (I also love the theme/design), I don’t have time to go through it all at the
moment but I have saved it and also included your RSS feeds, so when I
have time I will be back to read a great deal more, Please do keep up the awesome b.

_ plenty Of fish dating site of free dating ― 2015年11月06日 22:48

Hello there! This is my first visit to your blog!

We are a group of volunteers and starting a new project in a community in the same
niche. Your blog provided us beneficial information to work on. You have done a extraordinary job!

_ match.com free trial ― 2015年11月08日 18:49

I&#39;m extremely impressed with your writing skills as
well as with the layout on your weblog. Is this a paid theme
or did you modify it yourself? Anyway keep up the nice quality writing, it is rare to see a great blog like
this one nowadays.

_ kroger feedback ― 2015年11月11日 19:07

If you would like to increase your familiarity only keep visiting this website and be updated with
the latest information posted here.

_ quest bars ― 2015年11月30日 20:07

Asking questions are truly good thing if you are not understanding anything fully, however this article provides fastidious understanding even.

_ www.krogerfeedback.com ― 2015年12月04日 16:26

Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to more added agreeable from you! By the way, how could
we communicate?

_ plenty of fish Dating site of free dating plenty of Fish dating site of free dating ― 2015年12月08日 05:55

I have been browsing on-line more than 3 hours as of late, yet I by
no means found any fascinating article like
yours. It is pretty price sufficient for me. In my opinion, if all
website owners and bloggers made just right content material as you did, the internet
might be much more helpful than ever before.

_ www.krogerfeedback.com ― 2015年12月10日 08:35

I have been surfing online more than 4 hours today, yet I
never found any interesting article like yours.
It&#39;s pretty worth enough for me. In my view, if all
site owners and bloggers made good content as you
did, the web will be much more useful than ever before.

_ descargar facebook ― 2015年12月19日 10:08

Please let me know if you&#39;re looking for a writer for your blog.

You have some really great posts and I believe I would be a good asset.

If you ever want to take some of the load off, I&#39;d really like to write some articles for your blog in exchange for a link back to mine.
Please send me an email if interested. Cheers!

_ Descargar Firefox ---- ― 2016年01月06日 14:51

Very good information. Lucky me I found your website by chance (stumbleupon).
I have book-marked it for later!

_ quest bars ― 2016年02月09日 13:49

Hey just wanted to give you a quick heads up. The text in your post
seem to be running off the screen in Internet explorer.
I&#39;m not sure if this is a format issue or something to do with internet browser compatibility but I thought
I&#39;d post to let you know. The layout look
great though! Hope you get the issue solved soon. Kudos

_ quest bars ― 2016年02月10日 06:42

Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your site?
My website is in the very same niche as yours and my users would definitely
benefit from a lot of the information you provide here. Please let me know
if this okay with you. Many thanks!

_ quest bars ― 2016年02月19日 23:32

Great post. I was checking constantly this blog and I am impressed!

Extremely useful information specially the last part
:) I care for such information a lot. I was looking for this particular info for a
long time. Thank you and good luck.

_ bernie sanders ― 2016年03月31日 23:14

I absolutely love your website.. Very nice colors &amp; theme.
Did you create this website yourself? Please reply back as I&#39;m
looking to create my own blog and want to find out where
you got this from or just what the theme is named. Thanks!

_ tinder dating site ― 2017年02月25日 22:45

I just could not go away your website before suggesting that I extremely enjoyed the usual info an individual provide to your
visitors? Is gonna be back often to inspect new posts

_ tinder dating site ― 2017年02月26日 09:52

Today, I went to the beach with my kids. I found a sea shell and gave
it to my 4 year old daughter and said &quot;You can hear the ocean if you put this to your ear.&quot; She placed
the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.

She never wants to go back! LoL I know this is completely off topic but I had to tell someone!

_ tinder dating site ― 2017年02月27日 13:03

Hello, everything is going well here and ofcourse every one
is sharing data, that&#39;s truly good, keep up writing.

_ tinder dating site ― 2017年03月01日 02:31

I&#39;m truly enjoying the design and layout of your site.
It&#39;s a very easy on the eyes which makes it much more
enjoyable for me to come here and visit more often. Did you hire out a designer to create
your theme? Great work!

_ tinder dating site app ― 2017年03月02日 08:28

When I initially commented I appear to have clicked the -Notify me when new comments are added- checkbox and from now on every time a comment is added I recieve 4
emails with the exact same comment. Perhaps there is a way you are able to
remove me from that service? Thanks!

_ minecraft ― 2017年03月02日 22:56

I think this is among the most significant information for me.
And i&#39;m glad reading your article. But wanna remark on some general things, The site style
is great, the articles is really nice : D. Good job, cheers

_ tinder dating site free search ― 2017年03月05日 04:14

Wow, marvelous blog layout! How long have you been blogging for?
you make blogging look easy. The overall look of your
website is fantastic, let alone the content!

_ tinder dating site free search ― 2017年03月05日 12:03

Amazing! This blog looks exactly like my old one!

It&#39;s on a completely different subject but it has pretty much the same layout and design.
Superb choice of colors!

_ solvang.mrdrain.com ― 2017年03月10日 02:09

Having read this I thought it was extremely enlightening.
I appreciate you taking the time and effort to put this article together.
I once again find myself spending a lot of time both reading and leaving comments.
But so what, it was still worthwhile!

_ tinyurl.com ― 2017年03月11日 02:38

Excellent post! We are linking to this particularly great
content on our site. Keep up the great writing.

_ tinyurl.com ― 2017年03月11日 03:33

When I originally commented I clicked the &quot;Notify me when new comments are added&quot; checkbox and
now each time a comment is added I get three e-mails with the same comment.
Is there any way you can remove me from that service?
Bless you!

_ tinyurl.com ― 2017年03月11日 16:31

Hi! I just wish to offer you a big thumbs up for your great
information you have got right here on this post. I&#39;ll be returning to your web site
for more soon.

_ tinyurl.com ― 2017年03月11日 23:25

I all the time used to read piece of writing
in news papers but now as I am a user of net therefore from now I am using net for articles or reviews,
thanks to web.

_ j.mp ― 2017年03月12日 11:04

It&#39;s amazing to visit this web page and reading the views
of all friends regarding this article, while I am also keen of getting experience.

_ tinder dating site ― 2017年03月13日 13:34

Pretty section of content. I just stumbled upon your site and in accession capital to assert that
I get in fact enjoyed account your blog posts. Anyway I&#39;ll
be subscribing to your feeds and even I achievement you
access consistently quickly.

_ tinder dating site app ― 2017年03月14日 07:03

Hi there! This article could not be written much better!

Looking through this article reminds me of my
previous roommate! He continually kept talking about this.
I am going to send this post to him. Fairly certain he&#39;s going to have a very good
read. Many thanks for sharing!

_ tinder dating site ― 2017年03月15日 13:12

I visit each day some sites and sites to read articles or
reviews, except this web site gives feature based
posts.

_ tinder dating site ― 2017年03月19日 02:19

This article will assist the internet viewers for creating new
website or even a blog from start to end.

_ tinder dating site ― 2017年03月19日 02:27

Excellent goods from you, man. I&#39;ve understand your stuff previous to
and you are just extremely wonderful. I actually like what you&#39;ve acquired here, really like what you&#39;re stating and the
way in which you say it. You make it entertaining and you still take care of to keep
it sensible. I can not wait to read far more from you.

This is actually a great website.

_ tinder dating site ― 2017年03月20日 20:37

Today, I went to the beachfront with my children. I found a
sea shell and gave it to my 4 year old daughter and said &quot;You can hear the ocean if you put this to your ear.&quot; She placed the shell to her ear
and screamed. There was a hermit crab inside and it pinched her ear.

She never wants to go back! LoL I know this is
totally off topic but I had to tell someone!

_ free dating sites no email required ― 2017年03月22日 02:59

It&#39;s going to be ending of mine day, except before end I am reading this wonderful piece
of writing to increase my knowledge.

_ free dating sites no fees ― 2017年03月22日 05:24

I visited various web sites except the audio quality for audio songs present at
this site is truly excellent.

_ free dating sites no fees ― 2017年03月22日 06:06

I am sure this paragraph has touched all the internet visitors, its really really pleasant post
on building up new webpage.

_ free dating sites no fees ― 2017年03月23日 10:19

Hello there! This article could not be written much better!
Looking through this post reminds me of my previous roommate!
He continually kept preaching about this. I&#39;ll send this article to him.
Pretty sure he will have a good read. Thanks for sharing!

_ 100% free dating site reviews ― 2017年03月23日 10:35

I do not even know how I ended up right here, however I assumed this put up used to be good.

I don&#39;t recognize who you might be however definitely you
are going to a well-known blogger if you are not already.
Cheers!

_ minecraft demo ― 2017年03月26日 00:30

This is really interesting, You are a very skilled blogger.

I have joined your feed and look forward to seeking more of your excellent post.
Also, I have shared your website in my social networks!

_ minecraft demo ― 2017年03月27日 00:05

Have you ever thought about including a little bit more than just your articles?
I mean, what you say is important and everything.
But just imagine if you added some great graphics or video clips to
give your posts more, &quot;pop&quot;! Your content is excellent but
with images and video clips, this website could certainly be one of
the most beneficial in its niche. Very good blog!

_ manicure ― 2017年03月30日 20:09

Wow, amazing blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your website is magnificent, let alone the content!

_ manicure ― 2017年03月30日 20:12

Simply wish to say your article is as astounding. The clarity in your post is simply nice and i can assume you are an expert on this subject.
Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming post.
Thanks a million and please carry on the gratifying work.

_ manicure ― 2017年03月30日 20:21

Every weekend i used to pay a quick visit this site, because i
wish for enjoyment, since this this web page conations actually good funny data too.

_ www.krogerfeedback.com ― 2017年04月29日 16:45

Hi, yes this paragraph is genuinely pleasant and I have learned lot of things from it concerning
blogging. thanks.

※コメントの受付件数を超えているため、この記事にコメントすることができません。

トラックバック

このエントリのトラックバックURL: http://kida.asablo.jp/blog/2014/12/07/7511781/tb