home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / volume.27 / text0025.txt < prev    next >
Encoding:
Text File  |  1992-05-20  |  3.1 KB  |  146 lines

  1. Submitted-by: rv@erix.ericsson.se (Robert Virding)
  2.  
  3. I have been implementing a set of string functions for a language
  4. which we have developed at our lab. (Erlang, it's a real-time
  5. concurrent high-level language for building large robust systems) As I
  6. had need of some regular expression matching I decided to take the REs
  7. and basic functions (match, sub, gsub and split) as defined in "The
  8. AWK Programming Language" by Aho, Kernighan and Weinberger.
  9.  
  10. All went well until I started looking at how "awk" handles newlines
  11. embedded in strings wrt beginning/end of string expressions. I
  12. discovered that all the "awk"s we had acted differently. I have tried:
  13.  
  14. awk    - the old "standard" one
  15. nawk    - a "new" one available on SUN (?), compatible with book
  16. gawk    - GNU awk
  17. mawk    - recently available from comp.sources.reviewed
  18.  
  19. Both gawk and mawk follow the book and conform to POSIX 1003.2 (draft
  20. 11) standard.
  21.  
  22. I enclose some small test files which show how inconsistent the
  23. systems really are (or seem to be). Even when there were no newlines
  24. in the string results were inconsistent. What does the standard say?
  25. Any help would truly be appreciated.
  26.  
  27.  
  28.         Robert Virding  @ Ellemtel Utvecklings AB, Stockholm
  29.         EMAIL: rv@erix.ericsson.se
  30.  
  31. P.S. If there is any interest I will summarise the results.
  32.  
  33. TESTS AND RESULTS
  34. =================
  35.  
  36. Test 1
  37. ------
  38.  
  39. A simple test which all "awk"s could do. I must admit I can't really
  40. understand what gawk and mawk are doing. Nawk is closer to what I
  41. think would happen. But why isn't the 'f' removed in the 2nd case?
  42.  
  43. BEGIN {
  44.     s1 = "foobarbaz"
  45.     n = split(s1, a1, "^.")
  46.     print "n = " n
  47.     for (i = 1; i <= n; i++)
  48.         print ":" a1[i] ":"
  49.  
  50.     s2 = "foo\nbar\nbaz"
  51.     n = split(s2, a2, "^.")
  52.     print "n = " n
  53.     for (i = 1; i <= n; i++)
  54.         print ":" a2[i] ":"
  55.  
  56.     exit
  57. }
  58.  
  59. awk        nawk        gawk        mawk
  60.  
  61. n = 1        n = 2        n = 9        n = 10
  62. :foobarbaz:    ::        ::        ::
  63. n = 3        :oobarbaz:    ::        ::
  64. :foo:        n = 1        ::        ::
  65. :bar:        :foo        ::        ::
  66. :baz:        bar        ::        ::
  67.         baz:        ::        ::
  68.                 ::        ::
  69.                 ::        ::
  70.                 ::        ::
  71.                 n = 9        ::
  72.                 ::        n = 12
  73.                 ::        ::
  74.                 ::        ::
  75.                 :        ::
  76.                 :        ::
  77.                 ::        ::
  78.                 ::        ::
  79.                 :        ::
  80.                 :        ::
  81.                 ::        ::
  82.                 ::        ::
  83.                         ::
  84.                         ::
  85.  
  86. Test 2
  87. ------
  88.  
  89. This test was impossible for awk (no gsub function). I will also admit
  90. that nawk here seemd to do something which I feel is more reasonable.
  91.  
  92. BEGIN {
  93.     s1 = "foobarbaz"
  94.     gsub("^.", "X&Y", s1)
  95.     print ":" s1 ":"
  96.  
  97.     s2 = "foo\nbar\nbaz"
  98.     gsub("^.", "X&Y", s2)
  99.     print ":" s2 ":"
  100.  
  101.     exit
  102. }
  103.  
  104. nawk        gawk                mawk
  105.  
  106. :XfYoobarbaz:    :XfYXoYXoYXbYXaYXrYXbYXaYXzY:    :XfYXoYXoYXbYXaYXrYXbYXaYXzY:
  107. :XfYoo        :XfYXoYXoY            :XfYXoYXoYX
  108. bar        XbYXaYXrY            YXbYXaYXrYX
  109. baz:        XbYXaYXzY:            YXbYXaYXzY:
  110.  
  111.  
  112. Test 3
  113. ------
  114.  
  115. This test was impossible for awk (no match function). Mawk here seemed the
  116. best to me. Where nawk got its RLENGTH values from I can't understand.
  117.  
  118. BEGIN {
  119.     s1 = "foo"
  120.     match(s1, /foo$/)
  121.     print RSTART, RLENGTH
  122.     print ":" substr(s1, RSTART, RLENGTH) ":"
  123.  
  124.     s2 = "foo\n\n"
  125.     match(s2, /foo..$/)
  126.     print RSTART, RLENGTH
  127.     print ":" substr(s2, RSTART, RLENGTH) ":"
  128.  
  129.     exit
  130. }
  131.  
  132. nawk            gawk            mawk
  133.  
  134. 1 4            1 3            1 3
  135. :foo:            :foo:            :foo:
  136. 1 6            0 -1            1 5
  137. :foo            ::            :foo
  138.  
  139. :                        :
  140.  
  141. The very end
  142.  
  143.  
  144. Volume-Number: Volume 27, Number 26
  145.  
  146.