Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/src/runtime/Builtin/unpackPS.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.



#include "haskell2c.h"

/* unpackPS :: Prelude.PackedString -> [Prelude.Char] */

C_HEADER(unpackPS)
{
  int length;
  NodePtr nodeptr,prevptr;
  Coninfo cinfo;
  char *srcptr;

  nodeptr = C_GETARG1(1);
  IND_REMOVE(nodeptr);
  cinfo =  GET_CONINFO(nodeptr);
  length = CONINFO_LARGESIZES(cinfo) * sizeof(Node) - CONINFO_LARGEEXTRA(cinfo);

  C_CHECK(length * nhc_sizeCons);

  nodeptr = C_GETARG1(1);
  IND_REMOVE(nodeptr);
  srcptr = (char*)&nodeptr[1+EXTRA];

  prevptr = (NodePtr)&nodeptr;
  while(length--) {
    int c = *srcptr++;
    *prevptr = (Node)nhc_mkCons(nhc_mkChar(c),0);
    prevptr = &((NodePtr)(*prevptr))[EXTRA+2];
  }
  *prevptr = (Node)nhc_mkNil();

  C_RETURN(nodeptr);
}	

/* unpackPSC ::  E PackedString -> [Char] */
NodePtr unpackPSC (NodePtr e)
{
  int length;
  NodePtr nodeptr,prevptr;
  Coninfo cinfo;
  char *srcptr;

  /* outer constructor is required only to fool FFI not to take string apart. */
  nodeptr = GET_POINTER_ARG1(e,1);
  IND_REMOVE(nodeptr);
  cinfo =  GET_CONINFO(nodeptr);
  length = CONINFO_LARGESIZES(cinfo) * sizeof(Node) - CONINFO_LARGEEXTRA(cinfo);

  C_CHECK(length * nhc_sizeCons);

  nodeptr = C_GETARG1(1);
  IND_REMOVE(nodeptr);
  srcptr = (char*)&nodeptr[1+EXTRA];

  prevptr = (NodePtr)&nodeptr;
  while(length--) {
    int c = *srcptr++;
    *prevptr = (Node)nhc_mkCons(nhc_mkChar(c),0);
    prevptr = &((NodePtr)(*prevptr))[EXTRA+2];
  }
  *prevptr = (Node)nhc_mkNil();

  return (nodeptr);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].