#include "cdefs.h"
#include "mem.h"
extern cptr object_methods[meth_max];
extern cptr string_methods[meth_max];
extern cptr character_methods[meth_max];
#include "cstring.h"
#include "cint.h"
extern cptr boolean_methods[meth_max];
#include "cfile.h"
extern cptr strpool_methods[meth_max];

extern cint boolean_True;
extern cint strpool_str_6;
extern cint object_Undefined;
extern cint boolean_False;
extern cint strpool_str_34;


cptr sstring_methods[meth_max];

cint sstring_alloc(cint sstring_self, cint sstring_strbuf){
  cint sstring_result, sstring_strln;
  cint t1, t2, t3, t4, t5;
  sstring_strln = cstring_strlen((cint)sstring_strbuf);
  t2 = sstring_strln == 1;
  if (t2){
    cstring_mem_free((cint)sstring_self);
    t3 = mem_malloc(36); ((cptr)t3)[0] = character_methods;
    t4 = character_alloc((cint)t3, (cint)cstring_lookup_char((cint)sstring_strbuf, (cint)0), (cint)sstring_strbuf);
    sstring_result = t4;
  }else{
    t5 = string_alloc((cint)sstring_self);
    sstring_result = t5;
    ((cptr)sstring_result)[5] /* representation*/ = strpool_str_34;
    ((cptr)sstring_result)[2] /* hashval*/ = cstring_strsum((cint)sstring_strbuf);
    ((cptr)sstring_result)[3] /* len*/ = sstring_strln;
    ((cptr)sstring_result)[8] /* buf*/ = sstring_strbuf;
    ((cptr)sstring_result)[9] /* elem*/ = 0;
  };
  t1 = sstring_result;
  goto t1_label;
  t1_label: return t1;
}

cint sstring_stalloc(cint sstring_self, cint sstring_strbuf){
  cint sstring_result, sstring_strln;
  cint t6, t7;
  sstring_strln = cstring_strlen((cint)sstring_strbuf);
  t7 = string_alloc((cint)sstring_self);
  sstring_result = t7;
  ((cptr)sstring_result)[5] /* representation*/ = strpool_str_34;
  ((cptr)sstring_result)[2] /* hashval*/ = cstring_strsum((cint)sstring_strbuf);
  ((cptr)sstring_result)[3] /* len*/ = sstring_strln;
  ((cptr)sstring_result)[8] /* buf*/ = sstring_strbuf;
  ((cptr)sstring_result)[9] /* elem*/ = 0;
  t6 = sstring_result;
  goto t6_label;
  t6_label: return t6;
}

cint sstring_reclaim(cint sstring_self){
  cint t8, t9;
  cstring_mem_free((cint)((cptr)sstring_self)[8] /* buf*/);
  t9 = ((cptr)sstring_self)[9] /* elem*/ != 0;
  if (t9){
    object_Dec_ref(((cptr)sstring_self)[9] /* elem*/);
  };
  string_reclaim((cint)sstring_self);
  t8_label: return t8;
}

cint sstring_Contains(cint sstring_self, cint sstring_ob){
  cint sstring_result;
  cint t10, t11, t12, t13, t14;
  t11 = ((cptr)sstring_ob)[4] /* class_of*/ == strpool_str_6;
  t12 = t11;
  if (t12){
    t13 = ((cptr)sstring_ob)[3] /* len*/ == 1;
    t12 = t13;
  };
  if (t12){
    t14 = cstring_strchr((cint)((cptr)sstring_self)[8] /* buf*/, (cint)((cptr)sstring_ob)[2] /* hashval*/) != 0;
    if (t14){
      object_Dec_ref(sstring_self);
      object_Dec_ref(sstring_ob);
      t10 = boolean_True;
      goto t10_label;
    };
  };
  object_Dec_ref(sstring_self);
  object_Dec_ref(sstring_ob);
  t10 = boolean_False;
  goto t10_label;
  t10_label: return t10;
}

cint sstring_copy_of(cint sstring_self){
  cint sstring_result, sstring_strbuf;
  cint t15, t16, t17, t18;
  t16 = ((cptr)sstring_self)[1] /* refcount*/ <= 1;
  if (t16){
    t15 = sstring_self;
    goto t15_label;
  };
  sstring_strbuf = cstring_strdupl((cint)((cptr)sstring_self)[8] /* buf*/);
  t17 = mem_malloc(40); ((cptr)t17)[0] = sstring_methods;
  t18 = sstring_alloc((cint)t17, (cint)sstring_strbuf);
  sstring_result = t18;
  object_Dec_ref(sstring_self);
  t15 = sstring_result;
  goto t15_label;
  t15_label: return t15;
}

cint sstring_equal(cint sstring_self, cint sstring_s){
  cint sstring_result, sstring_i;
  cint t19, t20, t21, t22, t23, t24, t25, t27, t28, t29, t30;
  t20 = sstring_self == sstring_s;
  if (t20){
    t19 = 1;
    goto t19_label;
  };
  t21 = ((cptr)sstring_s)[4] /* class_of*/ != strpool_str_6;
  if (t21){
    t19 = 0;
    goto t19_label;
  };
  t22 = ((cptr)sstring_self)[2] /* hashval*/ != ((cptr)sstring_s)[2] /* hashval*/;
  if (t22){
    t19 = 0;
    goto t19_label;
  };
  t23 = ((cptr)sstring_self)[3] /* len*/ != ((cptr)sstring_s)[3] /* len*/;
  if (t23){
    t19 = 0;
    goto t19_label;
  };
  t24 = ((cptr)sstring_s)[5] /* representation*/ == strpool_str_34;
  if (t24){
    t25 = cstring_strcmp((cint)((cptr)sstring_self)[8] /* buf*/, (cint)((cptr)sstring_s)[8] /* buf*/) == 0;
    if (t25){
      t19 = 1;
      goto t19_label;
    };
    t19 = 0;
    goto t19_label;
  };
  for (sstring_i = 1; sstring_i <= ((cptr)sstring_self)[3] /* len*/; sstring_i++){
    t27 = ((cptr)(((cptr)sstring_s)[0]))[63];
    t28 = ((f0)t27)((cint)sstring_s, (cint)sstring_i); /* lookup_as_cint*/
    t29 = sstring_i - 1;
    t30 = t28 != cstring_lookup_char((cint)((cptr)sstring_self)[8] /* buf*/, (cint)t29);
    if (t30){
      t19 = 0;
      goto t19_label;
    };
  };t26:;
  t19 = 1;
  goto t19_label;
  t19_label: return t19;
}

cint sstring_Locase(cint sstring_self){
  cint sstring_result;
  cint t31, t32, t33, t34;
  t32 = ((cptr)(((cptr)sstring_self)[0]))[21];
  t33 = ((f0)t32)((cint)sstring_self); /* copy_of*/
  sstring_result = t33;
  ((cptr)sstring_result)[8] /* buf*/ = cstring_strlwr((cint)((cptr)sstring_result)[8] /* buf*/);
  ((cptr)sstring_result)[2] /* hashval*/ = cstring_strsum((cint)((cptr)sstring_result)[8] /* buf*/);
  t31 = sstring_result;
  goto t31_label;
  t31_label: return t31;
}

cint sstring_less_than(cint sstring_self, cint sstring_s){
  cint sstring_result, sstring_i, sstring_ch1, sstring_ch2, sstring_j;
  cint t35, t36, t37, t38, t39, t40, t41, t42, t44, t45, t46, t47, t48, t49, t50, t51, t52;
  t36 = ((cptr)sstring_s)[4] /* class_of*/ != strpool_str_6;
  if (t36){
    t35 = 0;
    goto t35_label;
  };
  t37 = ((cptr)(((cptr)sstring_s)[0]))[37];
  t38 = ((f0)t37)((cint)sstring_s, (cint)sstring_self); /* equal*/
  t39 = t38 == 1;
  if (t39){
    t35 = 0;
    goto t35_label;
  };
  t40 = ((cptr)sstring_s)[5] /* representation*/ == strpool_str_34;
  if (t40){
    t41 = cstring_strcmp((cint)((cptr)sstring_self)[8] /* buf*/, (cint)((cptr)sstring_s)[8] /* buf*/) < 0;
    if (t41){
      t35 = 1;
      goto t35_label;
    };
    t35 = 0;
    goto t35_label;
  };
  sstring_i = ((cptr)sstring_self)[3] /* len*/;
  t42 = sstring_i > ((cptr)sstring_s)[3] /* len*/;
  if (t42){
    sstring_i = ((cptr)sstring_s)[3] /* len*/;
  };
  for (sstring_j = 1; sstring_j <= sstring_i; sstring_j++){
    t44 = ((cptr)(((cptr)sstring_self)[0]))[63];
    t45 = ((f0)t44)((cint)sstring_self, (cint)sstring_j); /* lookup_as_cint*/
    sstring_ch1 = t45;
    t47 = ((cptr)(((cptr)sstring_s)[0]))[63];
    t48 = ((f0)t47)((cint)sstring_s, (cint)sstring_j); /* lookup_as_cint*/
    sstring_ch2 = t48;
    t50 = sstring_ch1 < sstring_ch2;
    if (t50){
      t35 = 1;
      goto t35_label;
    };
    t51 = sstring_ch1 > sstring_ch2;
    if (t51){
      t35 = 0;
      goto t35_label;
    };
  };t43:;
  t52 = sstring_i < ((cptr)sstring_s)[3] /* len*/;
  if (t52){
    t35 = 1;
    goto t35_label;
  };
  t35 = 0;
  goto t35_label;
  t35_label: return t35;
}

cint sstring_lookup_as_cint(cint sstring_self, cint sstring_i){
  cint sstring_result;
  cint t53, t54;
  t54 = sstring_i - 1;
  t53 = cstring_lookup_char((cint)((cptr)sstring_self)[8] /* buf*/, (cint)t54);
  goto t53_label;
  t53_label: return t53;
}

cint sstring_lookup(cint sstring_self, cint sstring_i){
  cint sstring_result;
  cint t55, t56, t57, t58, t59, t60, t61, t62, t63, t64, t65, t66, t67, t68, t69, t70, t71, t72, t73;
  t56 = sstring_i < 1;
  t57 = t56;
  if (!t57){    t58 = sstring_i > ((cptr)sstring_self)[3] /* len*/;
    t57 = t58;
  };
  if (t57){
    t55 = object_Undefined;
    goto t55_label;
  };
  t59 = ((cptr)sstring_self)[9] /* elem*/ == 0;
  if (t59){
    t60 = mem_malloc(36); ((cptr)t60)[0] = character_methods;
    t61 = ((cptr)(((cptr)sstring_self)[0]))[63];
    t62 = ((f0)t61)((cint)sstring_self, (cint)sstring_i); /* lookup_as_cint*/
    t63 = character_alloc((cint)t60, (cint)t62, (cint)0);
    ((cptr)sstring_self)[9] /* elem*/ = t63;
  }else{
  t65 = ((cptr)((cptr)sstring_self)[9] /* elem*/)[1] /* refcount*/ > 1;
  if (t65){
    object_Dec_ref(((cptr)sstring_self)[9] /* elem*/);
    t66 = mem_malloc(36); ((cptr)t66)[0] = character_methods;
    t67 = ((cptr)(((cptr)sstring_self)[0]))[63];
    t68 = ((f0)t67)((cint)sstring_self, (cint)sstring_i); /* lookup_as_cint*/
    t69 = character_alloc((cint)t66, (cint)t68, (cint)0);
    ((cptr)sstring_self)[9] /* elem*/ = t69;
  }else{
    t71 = ((cptr)(((cptr)sstring_self)[0]))[63];
    t72 = ((f0)t71)((cint)sstring_self, (cint)sstring_i); /* lookup_as_cint*/
    ((cptr)((cptr)sstring_self)[9] /* elem*/)[2] /* hashval*/ = t72;
  };};
  t55 = ((cptr)sstring_self)[9] /* elem*/;
  goto t55_label;
  t55_label: return t55;
}

cint sstring_put_on(cint sstring_self, cint sstring_f, cint sstring_i, cint sstring_j){
  cint sstring_k;
  cint t74, t75, t76, t77, t79, t80;
  t75 = sstring_i == 1;
  t76 = t75;
  if (t76){
    t77 = sstring_j == ((cptr)sstring_self)[3] /* len*/;
    t76 = t77;
  };
  if (t76){
    cfile_fputs((cint)((cptr)sstring_self)[8] /* buf*/, (cint)sstring_f);
  }else{
    t79 = sstring_i - 1;
    t80 = sstring_j - 1;
    for (sstring_k = t79; sstring_k <= t80; sstring_k++){
      cfile_fputc((cint)cstring_lookup_char((cint)((cptr)sstring_self)[8] /* buf*/, (cint)sstring_k), (cint)sstring_f);
    };t78:;
  };
  t74_label: return t74;
}

cint sstring_Upcase(cint sstring_self){
  cint sstring_result;
  cint t81, t82, t83, t84;
  t82 = ((cptr)(((cptr)sstring_self)[0]))[21];
  t83 = ((f0)t82)((cint)sstring_self); /* copy_of*/
  sstring_result = t83;
  ((cptr)sstring_result)[8] /* buf*/ = cstring_strupr((cint)((cptr)sstring_result)[8] /* buf*/);
  ((cptr)sstring_result)[2] /* hashval*/ = cstring_strsum((cint)((cptr)sstring_result)[8] /* buf*/);
  t81 = sstring_result;
  goto t81_label;
  t81_label: return t81;
}

cint sstring_update(cint sstring_self, cint sstring_i, cint sstring_ch){
  cint sstring_result;
  cint t85, t86, t87, t88, t89, t90, t91, t92, t93, t94, t95;
  t86 = sstring_i > ((cptr)sstring_self)[3] /* len*/;
  if (t86){
    t87 = string_update((cint)sstring_self, (cint)sstring_i, (cint)sstring_ch);
    t85 = t87;
    goto t85_label;
  };
  t88 = ((cptr)(((cptr)sstring_self)[0]))[21];
  t89 = ((f0)t88)((cint)sstring_self); /* copy_of*/
  sstring_result = t89;
  t91 = ((cptr)(((cptr)sstring_result)[0]))[63];
  t92 = ((f0)t91)((cint)sstring_result, (cint)sstring_i); /* lookup_as_cint*/
  t93 = t92 - sstring_ch;
  t94 = ((cptr)sstring_result)[2] /* hashval*/ - t93;
  ((cptr)sstring_result)[2] /* hashval*/ = t94;
  t95 = sstring_i - 1;
  cstring_update_char((cint)((cptr)sstring_result)[8] /* buf*/, (cint)t95, (cint)sstring_ch);
  t85 = sstring_result;
  goto t85_label;
  t85_label: return t85;
}

void init_sstring(){
  copy_meth_table(sstring_methods, string_methods);
  sstring_methods[0] = 40;
  sstring_methods[3] = sstring_alloc;
  sstring_methods[163] = sstring_stalloc;
  sstring_methods[4] = sstring_reclaim;
  sstring_methods[20] = sstring_Contains;
  sstring_methods[21] = sstring_copy_of;
  sstring_methods[37] = sstring_equal;
  sstring_methods[127] = sstring_Locase;
  sstring_methods[60] = sstring_less_than;
  sstring_methods[63] = sstring_lookup_as_cint;
  sstring_methods[62] = sstring_lookup;
  sstring_methods[128] = sstring_put_on;
  sstring_methods[129] = sstring_Upcase;
  sstring_methods[130] = sstring_update;
}
