Testing latest pari + WASM + node.js... and it works?! Wow.
License: GPL3
ubuntu2004
/* Copyright (C) 2018 The PARI group.12This file is part of the PARI/GP package.34PARI/GP is free software; you can redistribute it and/or modify it under the5terms of the GNU General Public License as published by the Free Software6Foundation; either version 2 of the License, or (at your option) any later7version. It is distributed in the hope that it will be useful, but WITHOUT8ANY WARRANTY WHATSOEVER.910Check the License for details. You should have received a copy of it, along11with the package; see the file 'COPYING'. If not, write to the Free Software12Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */1314#include "pari.h"15#include "paripriv.h"1617/********************************************************************/18/** **/19/** CHARACTER STRINGS **/20/** **/21/********************************************************************/2223/* Utillity functions */24char *25stack_strdup(const char *s)26{27long n = strlen(s)+1;28char *t = stack_malloc(n);29memcpy(t,s,n); return t;30}31char *32stack_strcat(const char *s, const char *t)33{34long ls = strlen(s), lt = strlen(t);35long n = ls + lt + 1;36char *u = stack_malloc(n);37memcpy(u, s, ls);38memcpy(u + ls,t, lt+1); return u;39}4041char *42pari_strdup(const char *s)43{44long n = strlen(s)+1;45char *t = (char*)pari_malloc(n);46memcpy(t,s,n); return t;47}48char *49pari_strndup(const char *s, long n)50{51char *t = (char*)pari_malloc(n+1);52memcpy(t,s,n); t[n] = 0; return t;53}5455/* return the first n0 chars of s as a GEN [s may not be 0-terminated] */56GEN57strntoGENstr(const char *s, long n0)58{59long n = nchar2nlong(n0+1); /* +1 for trailing 0 */60GEN x = cgetg(n+1, t_STR);61char *t = GSTR(x);62x[n] = 0; /* avoid uninitialized memory */63strncpy(t, s, n0); t[n0] = 0; return x;64}6566/* strntoGENstr would trigger gcc-8 stringop-truncation warning */67GEN68strtoGENstr(const char *s)69{70long n0 = strlen(s) + 1, n = nchar2nlong(n0);71GEN x = cgetg(n+1, t_STR);72char *t = GSTR(x);73x[n] = 0; strncpy(t, s, n0); return x;74}7576GEN77chartoGENstr(char c)78{79GEN x = cgetg(2, t_STR);80char *t = GSTR(x);81t[0] = c; t[1] = 0; return x;82}8384const char *85type_name(long t)86{87const char *s;88switch(t)89{90case t_INT : s="t_INT"; break;91case t_REAL : s="t_REAL"; break;92case t_INTMOD : s="t_INTMOD"; break;93case t_FRAC : s="t_FRAC"; break;94case t_FFELT : s="t_FFELT"; break;95case t_COMPLEX: s="t_COMPLEX"; break;96case t_PADIC : s="t_PADIC"; break;97case t_QUAD : s="t_QUAD"; break;98case t_POLMOD : s="t_POLMOD"; break;99case t_POL : s="t_POL"; break;100case t_SER : s="t_SER"; break;101case t_RFRAC : s="t_RFRAC"; break;102case t_QFB : s="t_QFB"; break;103case t_VEC : s="t_VEC"; break;104case t_COL : s="t_COL"; break;105case t_MAT : s="t_MAT"; break;106case t_LIST : s="t_LIST"; break;107case t_STR : s="t_STR"; break;108case t_VECSMALL:s="t_VECSMALL";break;109case t_CLOSURE: s="t_CLOSURE"; break;110case t_ERROR: s="t_ERROR"; break;111case t_INFINITY:s="t_INFINITY";break;112default: pari_err_BUG("type"); s = NULL; /* LCOV_EXCL_LINE */113}114return s;115}116117GEN118type0(GEN x)119{120const char *s = type_name(typ(x));121return strtoGENstr(s);122}123124static char125ltoc(long n) {126if (n <= 0 || n > 255)127pari_err(e_MISC, "out of range in integer -> character conversion (%ld)", n);128return (char)n;129}130static char131itoc(GEN x) { return ltoc(gtos(x)); }132133GEN134pari_strchr(GEN g)135{136long i, l, len, t = typ(g);137char *s;138GEN x;139if (is_vec_t(t)) {140l = lg(g); len = nchar2nlong(l);141x = cgetg(len+1, t_STR); s = GSTR(x);142for (i=1; i<l; i++) *s++ = itoc(gel(g,i));143}144else if (t == t_VECSMALL)145{146l = lg(g); len = nchar2nlong(l);147x = cgetg(len+1, t_STR); s = GSTR(x);148for (i=1; i<l; i++) *s++ = ltoc(g[i]);149}150else151return chartoGENstr(itoc(g));152*s = 0; return x;153}154155GEN156strsplit(GEN x, GEN p)157{158long i0, i, iv, ls, lt;159char *s, *t;160GEN v;161if (typ(x) != t_STR) pari_err_TYPE("strsplit",x);162s = GSTR(x); ls = strlen(s);163if (!p) lt = 0;164else165{166if (typ(p) != t_STR) pari_err_TYPE("strsplit",p);167t = GSTR(p); lt = strlen(t);168}169if (!lt) /* empty separator: split by char */170{171v = cgetg(ls+1, t_VEC);172for (i = 1; i <= ls; i++) gel(v,i) = chartoGENstr(s[i-1]);173return v;174}175v = cgetg(ls + 2, t_VEC); iv = 1;176for (i = i0 = 0; i < ls; i++)177while (!strncmp(s + i, t, lt))178{179gel(v, iv++) = strntoGENstr(s + i0, i - i0);180i += lt; i0 = i;181}182gel(v, iv++) = strntoGENstr(s + i0, i - i0);183stackdummy((pari_sp)(v + iv), (pari_sp)(v + ls + 2));184setlg(v, iv); return v;185}186187GEN188strjoin(GEN v, GEN p)189{190pari_sp av = avma;191long i, l;192GEN w;193if (!is_vec_t(typ(v))) pari_err_TYPE("strjoin",v);194if (!p) p = strtoGENstr("");195if (typ(p) != t_STR) pari_err_TYPE("strjoin",p);196l = lg(v); if (l == 1) return strtoGENstr("");197w = cgetg(2*l - 2, t_VEC);198gel(w, 1) = gel(v, 1);199for (i = 2; i < l; i++)200{201gel(w, 2*i-2) = p;202gel(w, 2*i-1) = gel(v, i);203}204return gerepileuptoleaf(av, shallowconcat1(w));205}206207208