Subversion Repositories Kolibri OS

Rev

Rev 9439 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef INCLUDE_STRING_H
  2. #define INCLUDE_STRING_H
  3.  
  4. #ifndef INCLUDE_MEM_H
  5. #include "../lib/mem.h"
  6. #endif
  7.  
  8. //------------------------------------------------------------------------------
  9. // strspn(dword text1,text2) --- example: strspn("12 year","1234567890") -> return 2
  10. // strpbrk(dword text1,text2) --- example: strpbrk("this test", " ckfi") -> return "is test"
  11. // strcmp( ESI, EDI)
  12. // strlen( EDI)
  13. // utf8_strlen( ESI)
  14. // strcpy( EDI, ESI) --- 0 if ==
  15. // strlcpy(dword text1,text2,signed length)
  16. // strcat( EDI, ESI)
  17. // chrcat(ESI, BL)
  18. // strncat(dword text1,text2,signed length) --- pasting the text of a certain length
  19. // strchr( ESI,BL) --- find first BL
  20. // strrchr( ESI,BL) --- find last BL
  21. // strstr( EBX, EDX)
  22. // itoa(signed long number) --- convert the number as a string
  23. // atoi(dword text) --- convert a string as a number
  24. // strupr( ESI)
  25. // strlwr( ESI) --- Cyrillic symbols may not work
  26. // strttl( EDX)
  27. // strtok( ESI)
  28. // strltrim(dword text) --- removes "blank" characters on the left (\r, \n and space)
  29. // strrtrim(dword text) --- removes "blank" characters on the right (\r, \n and space)
  30. // strtrim(dword text) --- delete "empty" characters (\ r \ n and space) on both sides
  31. // chrnum(dword searchin, char symbol)
  32. // strcpyb(dword searchin, copyin, startstr, endstr) --- copy string between strings
  33. // strnumb(dword searchin, startstr, endstr) --- get number between strings
  34. // strdup(dword text) --- allocation under the text
  35. //------------------------------------------------------------------------------
  36.  
  37. /*
  38. inline fastcall signed int strcmp( ESI, EDI)
  39. {
  40.     loop()
  41.     {
  42.         IF (DSBYTE[ESI]<DSBYTE[EDI]) RETURN -1;
  43.         IF (DSBYTE[ESI]>DSBYTE[EDI]) RETURN 1;
  44.         IF (DSBYTE[ESI]=='\0') RETURN 0;
  45.         ESI++;
  46.         EDI++;
  47.     }
  48. }
  49. */
  50.  
  51.  
  52.  
  53. inline int strspn(dword text1,text2)
  54. {
  55.         dword beg;
  56.         char s1,s2;
  57.         int ret;
  58.         ret = 0;
  59.         beg = text2;
  60.         do {
  61.                 s1 = ESBYTE[text1];
  62.                 text2 = beg;
  63.                 do {
  64.                         s2 = ESBYTE[text2];
  65.                         if(s1==s2)
  66.                         {
  67.                                 if(!s2)break;
  68.                                 $inc ret
  69.                                 break;
  70.                         }
  71.                         else $inc text2
  72.                 } while(s2);
  73.                 $inc text1
  74.         } while(s1);
  75.         return ret;
  76. }
  77.  
  78. inline dword strpbrk(dword text1,text2)
  79. {
  80.         char s,ss;
  81.         dword beg;
  82.         beg = text2;
  83.         do {
  84.                 s = ESBYTE[text1];
  85.                 text2 = beg;
  86.                 do {
  87.                         ss = ESBYTE[text2];
  88.                         if(ss==s) return text1;
  89.                         $inc text2
  90.                 } while(ss);
  91.                 $inc text1
  92.         } while(s);
  93.         return text1;
  94. }
  95.  
  96. inline fastcall signed int strncmp( ESI, EDI, ECX)
  97. {
  98.   asm {
  99.     MOV EBX, EDI
  100.     XOR EAX, EAX
  101.     MOV EDX, ECX
  102.     OR ECX, ECX
  103.     JE L1
  104.     REPNE SCASB
  105.     SUB EDX, ECX
  106.     MOV ECX, EDX
  107.     MOV EDI, EBX
  108.     XOR EBX, EBX
  109.     REPE CMPSB
  110.     MOV AL, DSBYTE[ ESI-1]
  111.     MOV BL, DSBYTE[ EDI-1]
  112.     SUB EAX, EBX
  113. L1:
  114.   }
  115. }
  116.  
  117. /*
  118. signed int strncmp(dword s1, s2, signed n)
  119. unsigned char _s1,_s2;
  120. {
  121.         if (n == 0)
  122.                 return 0;
  123.         do {
  124.                 _s1 = DSBYTE[s1];
  125.                 _s2 = DSBYTE[s2];
  126.                 if (_s1 != _s2)
  127.                 {
  128.                         $dec s2
  129.                         return _s1 - _s2;
  130.                 }
  131.                 $inc s2
  132.                 if (_s1 == 0)
  133.                         break;
  134.                 $inc s1
  135.                 $dec n
  136.         } while (n);
  137.         return 0;
  138. }
  139. */
  140.  
  141. /*
  142. inline signed int strncmp(dword text1,text2,len)
  143. {
  144.        
  145.         loop()
  146.         {
  147.                 if(DSBYTE[text1]!=DSBYTE[text2])return text1-text2;
  148.                 $dec len
  149.                 if(!len)return 0;
  150.         }
  151. }
  152. */
  153. inline fastcall unsigned int strlen( EDI)
  154. {
  155.         $xor eax, eax
  156.         $mov ecx, -1
  157.         $REPNE $SCASB
  158.         EAX-=2+ECX;
  159. }
  160.  
  161. inline dword strnlen(dword str, dword maxlen)
  162. {
  163.         dword cp;
  164.         for (cp = str; (maxlen != 0) && (DSBYTE[cp] != '\0'); cp++, maxlen--);
  165.         return cp - str;
  166. }
  167.  
  168. inline fastcall unsigned int utf8_strlen( ESI)
  169. {
  170.  $xor  ecx, ecx
  171.   _loop:
  172.  $lodsb
  173.  $test  al, al
  174.  $jz  _done
  175.  $and al, 0xc0
  176.  $cmp al, 0x80
  177.  $jz  _loop
  178.  $inc ecx
  179.  $jmp _loop
  180.  
  181.   _done:
  182.  return ECX;
  183. }
  184.  
  185. inline signed int strcmp(dword text1, text2)
  186. {
  187.         char s1,s2;
  188.         dword p1,p2;
  189.         p1 = text1;
  190.         p2 = text2;
  191.         loop()
  192.         {
  193.                 s1 = DSBYTE[text1];
  194.                 s2 = DSBYTE[text2];
  195.                 if(s1==s2)
  196.                 {
  197.                         if(s1==0) return 0;
  198.                 }
  199.                 else {
  200.                         return s1-s2;
  201.                 }
  202.                 $inc text1
  203.                 $inc text2
  204.         }
  205.         return 0;
  206. }
  207.  
  208.  
  209. inline fastcall bool streq(ESI, EDI)
  210. {
  211.         loop()
  212.         {
  213.                 if(DSBYTE[ESI]==DSBYTE[EDI])
  214.                 {
  215.                         if(DSBYTE[ESI]==0) return true;
  216.                 }
  217.                 else {
  218.                         return false;
  219.                 }
  220.                 ESI++;
  221.                 EDI++;
  222.         }
  223. }
  224.  
  225. inline fastcall bool streqrp(ESI, EDI) //streq right part
  226. {
  227.         if(DSBYTE[ESI]==0) || (DSBYTE[EDI]==0) return false;
  228.         loop()
  229.         {
  230.                 if(DSBYTE[EDI]==0) return true;
  231.                 if(DSBYTE[ESI]!=DSBYTE[EDI]) return false;
  232.                 ESI++;
  233.                 EDI++;
  234.         }
  235. }
  236.  
  237. inline fastcall void strcpy( EDI, ESI)
  238. {
  239.     $cld
  240. L2:
  241.     $lodsb
  242.     $stosb
  243.     $test al,al
  244.     $jnz L2
  245. }
  246.  
  247. inline fastcall int strlcpy(dword ESI, EDI, EBX)
  248. {
  249.     if (EBX<0) return -1;
  250.     EDX=0;
  251.     do {
  252.         DSBYTE[ESI]=DSBYTE[EDI];
  253.         ESI++;
  254.         EDI++;
  255.         EDX++;
  256.         if (EDX==EBX) { DSBYTE[ESI]='\0'; return -1;}
  257.     } while(DSBYTE[EDI-1]!='\0');
  258.     return 0;
  259. }
  260.  
  261. :void strncpy(dword dst, src, len)
  262. {
  263.         while (len) && (ESBYTE[src])
  264.         {
  265.                 ESBYTE[dst] = ESBYTE[src];
  266.                 dst++;
  267.                 src++;
  268.                 len--;
  269.         }
  270.         ESBYTE[dst]=0;
  271. }
  272.  
  273. /*
  274. inline fastcall void strtrim( ESI)
  275. {
  276.     EDI = ESI;
  277.     do{
  278.         AL=DSBYTE[EDI];
  279.         if (AL != '\32') && (AL != '\13') && (AL != '\10')
  280.         {
  281.             DSBYTE[ESI]=AL;
  282.             $inc ESI
  283.         }
  284.          $inc EDI
  285.     }while(AL!=0);
  286.     DSBYTE[ESI] = '\0';
  287. }
  288. */
  289.  
  290. inline byte __isWhite(int s){ if (s==13)||(s==32)||(s==10)||(s==9) return true; return false; }
  291. inline void strltrim(dword text){
  292.         int s;
  293.         dword back_text;
  294.         back_text = text;
  295.         s = ESBYTE[text];
  296.         while(__isWhite(s))
  297.         {
  298.                 $inc text
  299.                 s = ESBYTE[text];
  300.         }
  301.         loop()
  302.         {
  303.                 ESBYTE[back_text] = s;
  304.                 $inc back_text
  305.                 if(!s) break;
  306.                 $inc text
  307.                 s = ESBYTE[text];
  308.         };
  309. }
  310.  
  311. inline void strrtrim(dword text)
  312. {
  313.         int s;
  314.         dword p;
  315.         do {
  316.                 s = ESBYTE[text];
  317.                 if(__isWhite(s))
  318.                 {
  319.                         p = text;
  320.                         while(__isWhite(s))
  321.                         {
  322.                                 $inc text;
  323.                                 s = ESBYTE[text];
  324.                         }
  325.                 }
  326.                 else $inc text
  327.         } while(s);
  328.         $dec text
  329.         s = ESBYTE[text];
  330.         if(__isWhite(s)) ESBYTE[p] = 0;
  331. }
  332.  
  333. inline void strtrim(dword text){
  334.         int s;
  335.         dword p,back_text;
  336.         back_text = text;
  337.         s = ESBYTE[text];
  338.         while(__isWhite(s))
  339.         {
  340.                 $inc text
  341.                 s = ESBYTE[text];
  342.         }
  343.         do {
  344.                 s = ESBYTE[text];
  345.                 if(__isWhite(s))
  346.                 {
  347.                         p = back_text;
  348.                         while(__isWhite(s))
  349.                         {
  350.                                 ESBYTE[back_text] = s;
  351.                                 $inc back_text
  352.                                 $inc text;
  353.                                 s = ESBYTE[text];
  354.                         }
  355.                 }
  356.                 else {
  357.                         ESBYTE[back_text] = s;
  358.                         $inc back_text
  359.                         $inc text
  360.                 }
  361.         } while(s);
  362.         $dec text
  363.         s = ESBYTE[text];
  364.         if(__isWhite(s)) ESBYTE[p] = 0;
  365. }
  366.  
  367. inline fastcall void strcat( EDI, ESI)
  368. {
  369.   asm {
  370.     mov ebx, edi
  371.     xor ecx, ecx
  372.     xor eax, eax
  373.     dec ecx
  374.     repne scasb
  375.     dec edi
  376.     mov edx, edi
  377.     mov edi, esi
  378.     xor ecx, ecx
  379.     xor eax, eax
  380.     dec ecx
  381.     repne scasb
  382.     xor ecx, 0ffffffffh
  383.     mov edi, edx
  384.     mov edx, ecx
  385.     mov eax, edi
  386.     shr ecx, 2
  387.     rep movsd
  388.     mov ecx, edx
  389.     and ecx, 3
  390.     rep movsb
  391.     mov eax, ebx
  392.     }
  393. }
  394.  
  395. :void strncat(dword dst, src, len)
  396. {
  397.         while (ESBYTE[dst]) && (len) {
  398.                 dst++;
  399.                 len--;
  400.         }
  401.         while (ESBYTE[src]) && (len>1) {
  402.                 ESBYTE[dst] = ESBYTE[src];
  403.                 dst++;
  404.                 src++;
  405.                 len--;
  406.         }
  407.         ESBYTE[dst] = 0;
  408. }
  409.  
  410. inline fastcall void chrcat(ESI, DI)
  411. {
  412.     while (ESBYTE[ESI]) ESI++;
  413.     ESBYTE[ESI] = DI;
  414.     ESI++;
  415.     ESBYTE[ESI] = 0;
  416. }
  417.  
  418. inline fastcall void chrncat(EDI, AL, EDX)
  419. {
  420.     while (ESBYTE[EDI]) && (EDX) {
  421.         EDI++;
  422.         EDX--;
  423.     }
  424.     ESBYTE[EDI] = AL;
  425.     EDI++;
  426.     ESBYTE[EDI] = 0;
  427. }
  428.  
  429. /*
  430. inline dword strchr(ESI, BL)
  431. {
  432.         loop()
  433.         {
  434.                 AL = DSBYTE[ESI];
  435.                 if(!AL)return 0;
  436.                 if(AL==BL)return ESI;
  437.                 ESI++;
  438.         }
  439. }
  440. */
  441.  
  442. /*
  443. :void chrncat(dword dst, unsigned char s, dword len)
  444. {
  445.         while (ESBYTE[dst]) && (len) {
  446.                 dst++;
  447.                 len--;
  448.         }
  449.         if (len>1) {
  450.                 ESBYTE[dst] = s;
  451.                 ESBYTE[dst+1] = 0;
  452.         }
  453. }
  454. */
  455.  
  456. inline dword strchr(dword shb;char s)
  457. {
  458.         char ss;
  459.         loop()
  460.         {
  461.                 ss = DSBYTE[shb];
  462.                 if(!ss)return 0;
  463.                 if(ss==s)return shb;
  464.                 shb++;
  465.         }
  466. }
  467.  
  468. inline dword strchrw(dword str, len)
  469. {
  470.         len += str;
  471.         loop()
  472.         {
  473.                 if(!DSBYTE[str])return 0;
  474.                 if (__isWhite(DSBYTE[str])) return str;
  475.                 str++;
  476.                 if (str >= len) return 0;
  477.         }
  478. }
  479.  
  480. inline fastcall signed int strrchr( ESI,BL)
  481. {
  482.     int jj=0, last=0;
  483.     do{
  484.         jj++;
  485.         $lodsb
  486.         IF(AL==BL) last=jj;
  487.     } while(AL!=0);
  488.     return last;
  489. }
  490.  
  491.  
  492. inline fastcall unsigned int chrnum( ESI, BL)
  493. {
  494.     int num = 0;
  495.     while(DSBYTE[ESI])
  496.     {
  497.         if (DSBYTE[ESI] == BL) num++;
  498.         ESI++;
  499.     }
  500.     return num;
  501. }
  502.  
  503. inline fastcall unsigned int chrlnum( ESI, BL, EDI)
  504. {
  505.     int num = 0;
  506.     while(DSBYTE[ESI]) && (EDI)
  507.     {
  508.         if (DSBYTE[ESI] == BL) num++;
  509.         ESI++;
  510.         EDI--;
  511.     }
  512.     return num;
  513. }
  514.  
  515. inline fastcall signed int strstr( EBX, EDX)
  516. {
  517.   asm {
  518.     MOV EDI, EDX
  519.     XOR ECX, ECX
  520.     XOR EAX, EAX
  521.     DEC ECX
  522.     REPNE SCASB
  523.     NOT ECX
  524.     DEC ECX
  525.     JE LS2
  526.     MOV ESI, ECX
  527.     XOR ECX, ECX
  528.     MOV EDI, EBX
  529.     DEC ECX
  530.     REPNE SCASB
  531.     NOT ECX
  532.     SUB ECX, ESI
  533.     JBE LS2
  534.     MOV EDI, EBX
  535.     LEA EBX, DSDWORD[ ESI-1]
  536. LS1: MOV ESI, EDX
  537.     LODSB
  538.     REPNE SCASB
  539.     JNE LS2
  540.     MOV EAX, ECX
  541.     PUSH EDI
  542.     MOV ECX, EBX
  543.     REPE CMPSB
  544.     POP EDI
  545.     MOV ECX, EAX
  546.     JNE LS1
  547.     LEA EAX, DSDWORD[ EDI-1]
  548.     JMP SHORT LS3
  549. LS2: XOR EAX, EAX
  550. LS3:
  551.   }
  552. }
  553.  
  554. inline int strnum(dword haystack, needle)
  555. {
  556.         int count = 0;
  557.         int needle_len = strlen(needle);
  558.         loop() {
  559.                 if (! haystack = strstr(haystack, needle)) break;
  560.                 haystack+=needle_len;
  561.                 count++;
  562.         }
  563.         return count;
  564. }
  565.  
  566. inline int strinum(dword haystack, needle)
  567. {
  568.         int count = 0;
  569.         int needle_len = strlen(needle);
  570.         loop() {
  571.                 if (! haystack = strstri(haystack, needle)) break;
  572.                 haystack+=needle_len;
  573.                 count++;
  574.         }
  575.         return count;
  576. }
  577.  
  578. inline signed int strcmpi(dword cmp1, cmp2)
  579. {
  580.     char si, ue;
  581.  
  582.     loop()
  583.     {
  584.         si = DSBYTE[cmp1];
  585.         ue = DSBYTE[cmp2];
  586.         if (si>='A') && (si<='Z') si +=32;
  587.         if (ue>='A') && (ue<='Z') ue +=32;
  588.         if (si != ue) return si-ue;
  589.         cmp1++;
  590.         cmp2++;
  591.         if ((DSBYTE[cmp1]=='\0') && (DSBYTE[cmp2]=='\0')) return 0;
  592.         if (DSBYTE[cmp1]=='\0') return -1;
  593.         if (DSBYTE[cmp2]=='\0') return 1;
  594.     }
  595. }
  596.  
  597. inline dword strstri(dword searchin, usestr_s)
  598. {
  599.     dword usestr_e = usestr_s;
  600.     char si, ue;
  601.  
  602.     while(DSBYTE[searchin])
  603.     {
  604.         si = DSBYTE[searchin];
  605.         ue = DSBYTE[usestr_e];
  606.         if (si>='A') && (si<='Z') si +=32;
  607.         if (ue>='A') && (ue<='Z') ue +=32;
  608.         if (si == ue) usestr_e++; else usestr_e = usestr_s;
  609.         searchin++;
  610.         if (DSBYTE[usestr_e]=='\0') return searchin;
  611.     }
  612.     return 0;
  613. }
  614.  
  615.  
  616. inline unsigned int strcpyb(dword search_in, copyin, startstr, endstr)
  617. {
  618.     dword startp, endp;
  619.     dword copyin_start_off = copyin;
  620.     if (startstr==0) startp = search_in; else startp = strstr(search_in, startstr) + strlen(startstr);
  621.     if (! endp = strstri(startp, endstr)) endp = startp+strlen(search_in);
  622.     //if (startp==endp) return 0;
  623.     do
  624.     {
  625.         DSBYTE[copyin] = DSBYTE[startp];
  626.         copyin++;
  627.         startp++;
  628.     }
  629.     while (startp<endp);
  630.     DSBYTE[copyin] = '\0';
  631.     return copyin_start_off;
  632. }
  633.  
  634.  
  635. /*void strcat(char *to, char *from)
  636. {
  637.     while(*to) to++;
  638.     while(*from)
  639.     {
  640.         *to = *from;
  641.         to++;
  642.         from++;
  643.     }
  644.     *to = '\0';
  645. }*/
  646.  
  647.  
  648. inline fastcall dword atoi( EDI)
  649. {
  650.     $push ebx
  651.     $push esi
  652.     ESI=EDI;
  653.     while (DSBYTE[ESI]==' ') ESI++;
  654.     if (DSBYTE[ESI]=='-') ESI++;
  655.     EAX=0;
  656.     while (DSBYTE[ESI]>='0') && (DSBYTE[ESI]<='9')
  657.     {
  658.         $xor ebx, ebx
  659.         EBX = DSBYTE[ESI]-'0';
  660.         EAX *= 10;
  661.         EAX += EBX;
  662.         ESI++;
  663.     }
  664.     IF (DSBYTE[EDI]=='-') -EAX;
  665.     $pop esi
  666.     $pop ebx
  667. }
  668.  
  669.  
  670.  
  671. inline fastcall strupr( ESI)
  672. {
  673.     do{
  674.         AL=DSBYTE[ESI];
  675.         IF(AL>='a')IF(AL<='z')DSBYTE[ESI]=AL&0x5f;
  676.         IF (AL>=160) && (AL<=175) DSBYTE[ESI] = AL - 32;    //à-ï
  677.         IF (AL>=224) && (AL<=239) DSBYTE[ESI] = AL - 80;    //à-ï
  678.          ESI++;
  679.     }while(AL!=0);
  680. }
  681.  
  682. inline fastcall strlwr( ESI)
  683. {
  684.     do{
  685.         $LODSB
  686.         IF(AL>='A')&&(AL<='Z'){
  687.             AL+=0x20;
  688.             DSBYTE[ESI-1]=AL;
  689.             CONTINUE;
  690.         }
  691.     }while(AL!=0);
  692. }
  693.  
  694. inline fastcall strttl( EDX)
  695. {
  696.     AL=DSBYTE[EDX];
  697.     IF(AL>='a')&&(AL<='z')DSBYTE[EDX]=AL&0x5f;
  698.     IF (AL>=160) && (AL<=175) DSBYTE[EDX] = AL - 32;    //à-ï
  699.     IF (AL>=224) && (AL<=239) DSBYTE[EDX] = AL - 80;    //à-ï
  700.     do{
  701.         EDX++;
  702.         AL=DSBYTE[EDX];
  703.         IF(AL>='A')&&(AL<='Z'){DSBYTE[EDX]=AL|0x20; CONTINUE;}
  704.         IF(AL>='€')&&(AL<='')DSBYTE[EDX]=AL|0x20; // -¯
  705.         IF (AL>=144) && (AL<=159) DSBYTE[EDX] = AL + 80;    //à-ï
  706.     }while(AL!=0);
  707. }
  708.  
  709. /*
  710. dword itoa( ESI)
  711. {
  712.     unsigned char buffer[11];
  713.     $pusha
  714.     EDI = #buffer;
  715.     ECX = 10;
  716.     if (ESI < 0)
  717.     {
  718.          $mov     al, '-'
  719.          $stosb
  720.          $neg     esi
  721.     }
  722.  
  723.     $mov     eax, esi
  724.     $push    -'0'
  725. F2:
  726.     $xor     edx, edx
  727.     $div     ecx
  728.     $push    edx
  729.     $test    eax, eax
  730.     $jnz     F2
  731. F3:
  732.     $pop     eax
  733.     $add     al, '0'
  734.     $stosb
  735.     $jnz     F3
  736.    
  737.     $mov     al, '\0'
  738.     $stosb
  739.  
  740.     $popa
  741.     return #buffer;
  742. }
  743. */
  744. :unsigned char BUF_ITOA[11];
  745. inline dword itoa(signed long number)
  746. {
  747.         dword ret,p;
  748.         byte cmd;
  749.         long mask,tmp;
  750.         mask = 1000000000;
  751.         cmd = true;
  752.         p = #BUF_ITOA;
  753.         if(!number){
  754.                 ESBYTE[p] = '0';
  755.                 ESBYTE[p+1] = 0;
  756.                 return p;
  757.         }
  758.         ret = p;
  759.         if(number<0)
  760.         {
  761.                 $neg number
  762.                 ESBYTE[p] = '-';
  763.                 $inc p
  764.         }
  765.         while(mask)
  766.         {
  767.                 tmp = number / mask;
  768.                 tmp = tmp%10;
  769.                
  770.                 if(cmd){
  771.                         if(tmp){
  772.                                 ESBYTE[p] = tmp + '0';
  773.                                 $inc p
  774.                                 cmd = false;
  775.                         }
  776.                 }
  777.                 else {
  778.                         ESBYTE[p] = tmp + '0';
  779.                         $inc p
  780.                 }
  781.                 mask /= 10;
  782.         }
  783.         ESBYTE[p] = 0;
  784.         return ret;
  785. }
  786.  
  787. inline fastcall itoa_(signed int EDI, ESI)
  788. {
  789.     $pusha
  790.     EBX = EDI;
  791.     ECX = 10;
  792.     if (ESI > 90073741824)
  793.     {
  794.          $mov     al, '-'
  795.          $stosb
  796.          $neg     esi
  797.     }
  798.  
  799.     $mov     eax, esi
  800.     $push    -'0'
  801. F2:
  802.     $xor     edx, edx
  803.     $div     ecx
  804.     $push    edx
  805.     $test    eax, eax
  806.     $jnz     F2
  807. F3:
  808.     $pop     eax
  809.     $add     al, '0'
  810.     $stosb
  811.     $jnz     F3
  812.    
  813.     $mov     al, '\0'
  814.     $stosb
  815.  
  816.     $popa
  817.     return EBX;
  818. }
  819.  
  820. inline dword memchr(dword s,int c,signed len)
  821. {
  822.         if(!len) return NULL;
  823.         do {
  824.                 if(DSBYTE[s] == c) return s;
  825.                 $inc s
  826.                 $dec len
  827.         } while(len);
  828.         return NULL;
  829. }
  830.  
  831. inline dword strdup(dword text)
  832. {
  833.     dword l = strlen(text);
  834.     dword ret = malloc(l+1);
  835.         if(!ret) return NULL;
  836.     strlcpy(ret,text,l);
  837.     return ret;
  838. }
  839.  
  840. inline dword strndup(dword str, signed maxlen)
  841. {
  842.         dword copy,len;
  843.  
  844.         len = strnlen(str, maxlen);
  845.         copy = malloc(len + 1);
  846.         if (copy != NULL)
  847.         {
  848.                 strlcpy(copy, str, len);
  849.                 DSBYTE[len+copy] = '\0';
  850.         }
  851.         return copy;
  852. }
  853.  
  854. inline dword hexdec(dword text)
  855. {
  856.         char s;
  857.         dword ret,l;
  858.         ret = 0;
  859.         s = DSBYTE[text];
  860.         while(s)
  861.         {      
  862.                 ret <<= 4;
  863.                 if(s>='A')&&(s<='F')ret |= s-'A'+10;
  864.                 else if(s>='a')&&(s<='f')ret |= s-'a'+10;
  865.                 else if(s>='0')&&(s<='9')ret |= s-'0';
  866.                 text++;
  867.                 s = DSBYTE[text];
  868.         }
  869.         return ret;
  870. }
  871.  
  872. inline signed csshexdec(dword text)
  873. {
  874.         char s;
  875.         dword ret,l;
  876.         byte tmp;
  877.         l = strlen(text);
  878.         ret = 0;
  879.         s = DSBYTE[text];
  880.         tmp = 0;
  881.         if(l==6) while(s)
  882.         {      
  883.                 ret <<= 4;
  884.                 if(s>='A')&&(s<='F')ret |= s-'A'+10;
  885.                 else if(s>='a')&&(s<='f')ret |= s-'a'+10;
  886.                 else if(s>='0')&&(s<='9')ret |= s-'0';
  887.                 text++;
  888.                 s = DSBYTE[text];
  889.         }
  890.         else if(l==3) while(s)
  891.         {      
  892.                 ret |= tmp;
  893.                 ret <<= 4;
  894.                 ret |= tmp;
  895.                 ret <<= 4;
  896.                 if(s>='A')&&(s<='F')tmp = s-'A'+10;
  897.                 else if(s>='a')&&(s<='f')tmp = s-'a'+10;
  898.                 else if(s>='0')&&(s<='9')tmp = s-'0';
  899.                 text++;
  900.                 s = DSBYTE[text];
  901.         }
  902.         return ret;
  903. }
  904.  
  905. :dword miniprintf(dword dst, format, insert_line)
  906. {
  907.         dword in_pos = strchr(format, '%');
  908.         EBX = ESBYTE[EAX+1];
  909.         if (EBX == 's') {
  910.                 strncpy(dst, format, in_pos - format);
  911.                 strcat(dst, insert_line);
  912.                 strcat(dst, in_pos+2);
  913.         }
  914.         if (EBX == 'd') || (EBX == 'i') {
  915.                 strncpy(dst, format, in_pos - format);
  916.                 strcat(dst, itoa(insert_line));
  917.                 strcat(dst, in_pos+2); 
  918.         }
  919.         return dst;
  920. }
  921.  
  922. inline cdecl int sprintf(dword buf, format,...)
  923. {
  924.         #define END_ARGS 0xFF00FF //ARGS FUNCTION
  925.         byte s;
  926.         dword ret, tmp, l;
  927.         dword arg = #format;
  928.         ret = buf;
  929.         s = DSBYTE[format];
  930.         while(s){
  931.                 if(s=='%'){
  932.                         arg+=4;
  933.                         tmp = DSDWORD[arg];
  934.                         if(tmp==END_ARGS)goto END_FUNC_SPRINTF;
  935.                         $inc format
  936.                         s = DSBYTE[format];
  937.                         if(!s)goto END_FUNC_SPRINTF;
  938.                         switch(s)
  939.                         {
  940.                                 case 's':
  941.                                         l = tmp;
  942.                                         s = DSBYTE[tmp];
  943.                                         while(s)
  944.                                         {
  945.                                                 DSBYTE[buf] = s;
  946.                                                 $inc tmp
  947.                                                 $inc buf
  948.                                                 s = DSBYTE[tmp];
  949.                                         }
  950.                                 break;
  951.                                 case 'c':
  952.                                         DSBYTE[buf] = tmp;
  953.                                         $inc buf
  954.                                 break;
  955.                                 case 'u': //if(tmp<0)return ret;
  956.                                 case 'd':
  957.                                 case 'i':
  958.                                         tmp = itoa(tmp);
  959.                                         if(!DSBYTE[tmp])goto END_FUNC_SPRINTF;
  960.                                         l = strlen(tmp);
  961.                                         strlcpy(buf,tmp,l);
  962.                                         buf += l;
  963.                                 break;
  964.                                 case 'a':
  965.                                 case 'A':
  966.                                         strlcpy(buf,"0x00000000",10);
  967.                                         buf+=10;
  968.                                         l=buf;
  969.                                         while(tmp)
  970.                                         {
  971.                                                 $dec buf
  972.                                                 s=tmp&0xF;
  973.                                                 if(s>9)DSBYTE[buf]='A'+s-10;
  974.                                                 else DSBYTE[buf]='0'+s;
  975.                                                 tmp>>=4;
  976.                                         }
  977.                                         buf=l;
  978.                                 break;
  979.                                 case 'p':
  980.                                         tmp = itoa(#tmp);
  981.                                         if(!DSBYTE[tmp])goto END_FUNC_SPRINTF;
  982.                                         l = strlen(tmp);
  983.                                         strlcpy(buf,tmp,l);
  984.                                         buf += l;
  985.                                 break;
  986.                                 case '%':
  987.                                         DSBYTE[buf] = '%';
  988.                                         $inc buf
  989.                                 break;
  990.                                 default:
  991.                                 goto END_FUNC_SPRINTF;
  992.                         }
  993.                 }
  994.                 else {
  995.                         DSBYTE[buf] = s;
  996.                         $inc buf
  997.                 }
  998.                 $inc format
  999.                 s = DSBYTE[format];
  1000.         }
  1001.         END_FUNC_SPRINTF:
  1002.         DSBYTE[buf] = 0;
  1003.         return ret;
  1004. }
  1005.  
  1006. inline signed strcoll(dword text1,text2)
  1007. {
  1008.         char s,ss;
  1009.         loop()
  1010.         {
  1011.                 s = DSBYTE[text2];
  1012.                 ss=strchr(text1,s);
  1013.                 if(ss)return ss;
  1014.                 text2++;
  1015.         }
  1016.         return 0;
  1017. }
  1018.  
  1019. // void * memset( ptr, value, num );
  1020. // fills the memory with a dword
  1021. // example: memset(str,'-', sizeof(str));
  1022. inline void MEMSETD(EDI,ECX,EAX)
  1023. {
  1024.         $REP
  1025.         $STOSD
  1026. }
  1027.  
  1028. :replace_char(dword in_str, char from_char, to_char, int length) {
  1029.         dword max = in_str + length;
  1030.         while (in_str < max) {
  1031.                 if (ESBYTE[in_str] == from_char) ESBYTE[in_str] = to_char;
  1032.                 in_str++;
  1033.         }
  1034. }
  1035.  
  1036. #endif