My c# transliteration from your c code. Not sure if I had done
it correctly.

the return 0 inside scriptclose seemed shouldn't be there.
I tested it works with some success and some failures
aa)       failed
aa))      ok 
aa())     ok

single ")" (non-scriptclose) doesn't work

aa(     failed
aa((    failed
aa(((   failed
aa(()   failed
aa((()  failed
aa((()) ok

it seems "(" requires at least 2 ")" to work.

Handling of contexts for inside string and after NB. seems
working correctly.

c#:
  int scriptclose(string line, int len)
  {
    int rparen= 0;
    for (int j= 0; j<len; j++) {
      char c= line[j];
      if (' '==c) continue;
      if ('\t'==c) continue;
      if (')'==c) {
        if (0!=rparen++) return 0;
      }
//        return 0;
    }
    return 1;
  }

  int firstunmatched(string line, int len)
  {
    if (0==len || 0!=scriptclose(line, len)) return len;
    int depth= 0;
    int nb= 0;
    int unquoted= 1;
    int uncommented= 1;
    int r= len;    /* first unmatched ) */
    int l= len;     /* first unmatched ( */
    int[] d=new int[len];
    for (int j= 0; j<len; j++) {
      char c= line[j];
      if ('\''==c) unquoted= (0==unquoted)?1:0;
      if (unquoted!=0 && uncommented!=0) {
        if ('('==c) depth++; /* find paren depth */
        else if (')'==c) depth--;
        if (-1==depth && r==len) r=j; /* find first unmatched ) */
        if (0==nb && 'N'==c) nb=1; /* find comment start */
        else if (1==nb && 'B'==c) nb=2;
        else if (2==nb && '.'==c) uncommented= 0;
        else nb= 0;
      }
      d[j]= depth;
    }
    if (0 < d[len-1]) {
      int prev= 0;
      for (int j= 0; j<r; j++) {
        if (0 ==prev && 1==d[j]) {
          l= j;
        }
        prev= d[j];
      }
      return l;
    }
    return r;
  }


Вт, 27 дек 2016, Raul Miller написал(а):
> Ok, here's a translation to C. Some variable names changed. Lightly tested:
> 
> int scriptclose(char *line, unsigned int len) {
>     int rparen= 0;
>     for (unsigned int j= 0; j<len; j++) {
>         char c= line[j];
>         if (' '==c) continue;
>         if ('\t'==c) continue;
>         if (')'==c) {
>             if (rparen++) return 0;
>         }
>         return 0;
>     }
>     return 1;
> }
> 
> int firstunmatched(char *line, unsigned int len) {
>     if (0==len || scriptclose(line, len)) return len;
>     int depth= 0;
>     int nb= 0;
>     int unquoted= 1;
>     int uncommented= 1;
>     int r= len;    /* first unmatched ) */
>     int l= len;     /* first unmatched ( */
>     int d[len];
>     for (unsigned int j= 0; j<len; j++) {
>         char c= line[j];
>                 if ('\''==c) unquoted=!unquoted;
>         if (unquoted && uncommented) {
>             if ('('==c) depth++; /* find paren depth */
>             else if (')'==c) depth--;
>             if (-1==depth && r==len) r=j; /* find first unmatched ) */
>             if (0==nb && 'N'==c) nb=1; /* find comment start */
>             else if (1==nb && 'B'==c) nb=2;
>             else if (2==nb && '.'==c) uncommented= 0;
>             else nb= 0;
>         }
>         d[j]= depth;
>     }
>     if (0 < d[len-1]) {
>         int prev= 0;
>         for (unsigned int j= 0; j<r; j++) {
>             if (0 ==prev && 1==d[j]) {
>                 l= j;
>             }
>             prev= d[j];
>         }
>         return l;
>     }
>     return r;
> }
> 
> Results should match that of:
> 
> firstunmatched=:3 :0
>   if. ')' -:&(-.&' ') y do. #y return. end.
>   q=. unquoted=. -.~:/\y=''''
>   c=. uncommented=. -. +./\ q * 'NB.' E. y
>   n=. parendepth=. +/\q*c*-/'()'=/y
>   if. 0 < {: n do.
>     (n i. _1) <. 1 i:~0 1 E. 0,n
>   else.
>     n i. _1
>   end.
> )
> 
> I hope this helps,
> 
> -- 
> Raul
> 
> On Mon, Dec 26, 2016 at 11:18 PM, bill lam <[email protected]> wrote:
> > The firstunmatched verb needs to run only on visible
> > portion of text and I think it should be fast enough, but
> > it requires some effort to translate the scan operator to
> > its equivalent in c or c#.
> >
> > --
> > regards,
> > ====================================================
> > GPG key 1024D/4434BAB3 2008-08-24
> > gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3
> > gpg --keyserver subkeys.pgp.net --armor --export 4434BAB3
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm

-- 
regards,
====================================================
GPG key 1024D/4434BAB3 2008-08-24
gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3
gpg --keyserver subkeys.pgp.net --armor --export 4434BAB3
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to