Subversion Repositories Kolibri OS

Rev

Rev 8302 | Rev 8484 | Go to most recent revision | 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.  
  504. inline fastcall signed int strstr( EBX, EDX)
  505. {
  506.   asm {
  507.     MOV EDI, EDX
  508.     XOR ECX, ECX
  509.     XOR EAX, EAX
  510.     DEC ECX
  511.     REPNE SCASB
  512.     NOT ECX
  513.     DEC ECX
  514.     JE LS2
  515.     MOV ESI, ECX
  516.     XOR ECX, ECX
  517.     MOV EDI, EBX
  518.     DEC ECX
  519.     REPNE SCASB
  520.     NOT ECX
  521.     SUB ECX, ESI
  522.     JBE LS2
  523.     MOV EDI, EBX
  524.     LEA EBX, DSDWORD[ ESI-1]
  525. LS1: MOV ESI, EDX
  526.     LODSB
  527.     REPNE SCASB
  528.     JNE LS2
  529.     MOV EAX, ECX
  530.     PUSH EDI
  531.     MOV ECX, EBX
  532.     REPE CMPSB
  533.     POP EDI
  534.     MOV ECX, EAX
  535.     JNE LS1
  536.     LEA EAX, DSDWORD[ EDI-1]
  537.     JMP SHORT LS3
  538. LS2: XOR EAX, EAX
  539. LS3:
  540.   }
  541. }
  542.  
  543. inline int strnum(dword haystack, needle)
  544. {
  545.         int count = 0;
  546.         int needle_len = strlen(needle);
  547.         loop() {
  548.                 if (! haystack = strstr(haystack, needle)) break;
  549.                 haystack+=needle_len;
  550.                 count++;
  551.         }
  552.         return count;
  553. }
  554.  
  555. inline int strinum(dword haystack, needle)
  556. {
  557.         int count = 0;
  558.         int needle_len = strlen(needle);
  559.         loop() {
  560.                 if (! haystack = strstri(haystack, needle)) break;
  561.                 haystack+=needle_len;
  562.                 count++;
  563.         }
  564.         return count;
  565. }
  566.  
  567. inline signed int strcmpi(dword cmp1, cmp2)
  568. {
  569.     char si, ue;
  570.  
  571.     loop()
  572.     {
  573.         si = DSBYTE[cmp1];
  574.         ue = DSBYTE[cmp2];
  575.         if (si>='A') && (si<='Z') si +=32;
  576.         if (ue>='A') && (ue<='Z') ue +=32;
  577.         if (si != ue) return si-ue;
  578.         cmp1++;
  579.         cmp2++;
  580.         if ((DSBYTE[cmp1]=='\0') && (DSBYTE[cmp2]=='\0')) return 0;
  581.         if (DSBYTE[cmp1]=='\0') return -1;
  582.         if (DSBYTE[cmp2]=='\0') return 1;
  583.     }
  584. }
  585.  
  586. inline dword strstri(dword searchin, usestr_s)
  587. {
  588.     dword usestr_e = usestr_s;
  589.     char si, ue;
  590.  
  591.     while(DSBYTE[searchin])
  592.     {
  593.         si = DSBYTE[searchin];
  594.         ue = DSBYTE[usestr_e];
  595.         if (si>='A') && (si<='Z') si +=32;
  596.         if (ue>='A') && (ue<='Z') ue +=32;
  597.         if (si == ue) usestr_e++; else usestr_e = usestr_s;
  598.         searchin++;
  599.         if (DSBYTE[usestr_e]=='\0') return searchin;
  600.     }
  601.     return 0;
  602. }
  603.  
  604.  
  605. inline unsigned int strcpyb(dword search_in, copyin, startstr, endstr)
  606. {
  607.     dword startp, endp;
  608.     dword copyin_start_off = copyin;
  609.     if (startstr==0) startp = search_in; else startp = strstr(search_in, startstr) + strlen(startstr);
  610.     if (! endp = strstri(startp, endstr)) endp = startp+strlen(search_in);
  611.     //if (startp==endp) return 0;
  612.     do
  613.     {
  614.         DSBYTE[copyin] = DSBYTE[startp];
  615.         copyin++;
  616.         startp++;
  617.     }
  618.     while (startp<endp);
  619.     DSBYTE[copyin] = '\0';
  620.     return copyin_start_off;
  621. }
  622.  
  623.  
  624. /*void strcat(char *to, char *from)
  625. {
  626.     while(*to) to++;
  627.     while(*from)
  628.     {
  629.         *to = *from;
  630.         to++;
  631.         from++;
  632.     }
  633.     *to = '\0';
  634. }*/
  635.  
  636.  
  637. inline fastcall dword atoi( EDI)
  638. {
  639.     $push ebx
  640.     $push esi
  641.     ESI=EDI;
  642.     while (DSBYTE[ESI]==' ') ESI++;
  643.     if (DSBYTE[ESI]=='-') ESI++;
  644.     EAX=0;
  645.     while (DSBYTE[ESI]>='0') && (DSBYTE[ESI]<='9')
  646.     {
  647.         $xor ebx, ebx
  648.         EBX = DSBYTE[ESI]-'0';
  649.         EAX *= 10;
  650.         EAX += EBX;
  651.         ESI++;
  652.     }
  653.     IF (DSBYTE[EDI]=='-') -EAX;
  654.     $pop esi
  655.     $pop ebx
  656. }
  657.  
  658.  
  659.  
  660. inline fastcall strupr( ESI)
  661. {
  662.     do{
  663.         AL=DSBYTE[ESI];
  664.         IF(AL>='a')IF(AL<='z')DSBYTE[ESI]=AL&0x5f;
  665.         IF (AL>=160) && (AL<=175) DSBYTE[ESI] = AL - 32;    //à-ï
  666.         IF (AL>=224) && (AL<=239) DSBYTE[ESI] = AL - 80;    //à-ï
  667.          ESI++;
  668.     }while(AL!=0);
  669. }
  670.  
  671. inline fastcall strlwr( ESI)
  672. {
  673.     do{
  674.         $LODSB
  675.         IF(AL>='A')&&(AL<='Z'){
  676.             AL+=0x20;
  677.             DSBYTE[ESI-1]=AL;
  678.             CONTINUE;
  679.         }
  680.     }while(AL!=0);
  681. }
  682.  
  683. inline fastcall strttl( EDX)
  684. {
  685.     AL=DSBYTE[EDX];
  686.     IF(AL>='a')&&(AL<='z')DSBYTE[EDX]=AL&0x5f;
  687.     IF (AL>=160) && (AL<=175) DSBYTE[EDX] = AL - 32;    //à-ï
  688.     IF (AL>=224) && (AL<=239) DSBYTE[EDX] = AL - 80;    //à-ï
  689.     do{
  690.         EDX++;
  691.         AL=DSBYTE[EDX];
  692.         IF(AL>='A')&&(AL<='Z'){DSBYTE[EDX]=AL|0x20; CONTINUE;}
  693.         IF(AL>='€')&&(AL<='')DSBYTE[EDX]=AL|0x20; // -¯
  694.         IF (AL>=144) && (AL<=159) DSBYTE[EDX] = AL + 80;    //à-ï
  695.     }while(AL!=0);
  696. }
  697.  
  698. /*
  699. dword itoa( ESI)
  700. {
  701.     unsigned char buffer[11];
  702.     $pusha
  703.     EDI = #buffer;
  704.     ECX = 10;
  705.     if (ESI < 0)
  706.     {
  707.          $mov     al, '-'
  708.          $stosb
  709.          $neg     esi
  710.     }
  711.  
  712.     $mov     eax, esi
  713.     $push    -'0'
  714. F2:
  715.     $xor     edx, edx
  716.     $div     ecx
  717.     $push    edx
  718.     $test    eax, eax
  719.     $jnz     F2
  720. F3:
  721.     $pop     eax
  722.     $add     al, '0'
  723.     $stosb
  724.     $jnz     F3
  725.    
  726.     $mov     al, '\0'
  727.     $stosb
  728.  
  729.     $popa
  730.     return #buffer;
  731. }
  732. */
  733. :unsigned char BUF_ITOA[11];
  734. inline dword itoa(signed long number)
  735. {
  736.         dword ret,p;
  737.         byte cmd;
  738.         long mask,tmp;
  739.         mask = 1000000000;
  740.         cmd = true;
  741.         p = #BUF_ITOA;
  742.         if(!number){
  743.                 ESBYTE[p] = '0';
  744.                 ESBYTE[p+1] = 0;
  745.                 return p;
  746.         }
  747.         ret = p;
  748.         if(number<0)
  749.         {
  750.                 $neg number
  751.                 ESBYTE[p] = '-';
  752.                 $inc p
  753.         }
  754.         while(mask)
  755.         {
  756.                 tmp = number / mask;
  757.                 tmp = tmp%10;
  758.                
  759.                 if(cmd){
  760.                         if(tmp){
  761.                                 ESBYTE[p] = tmp + '0';
  762.                                 $inc p
  763.                                 cmd = false;
  764.                         }
  765.                 }
  766.                 else {
  767.                         ESBYTE[p] = tmp + '0';
  768.                         $inc p
  769.                 }
  770.                 mask /= 10;
  771.         }
  772.         ESBYTE[p] = 0;
  773.         return ret;
  774. }
  775.  
  776. inline fastcall itoa_(signed int EDI, ESI)
  777. {
  778.     $pusha
  779.     EBX = EDI;
  780.     ECX = 10;
  781.     if (ESI > 90073741824)
  782.     {
  783.          $mov     al, '-'
  784.          $stosb
  785.          $neg     esi
  786.     }
  787.  
  788.     $mov     eax, esi
  789.     $push    -'0'
  790. F2:
  791.     $xor     edx, edx
  792.     $div     ecx
  793.     $push    edx
  794.     $test    eax, eax
  795.     $jnz     F2
  796. F3:
  797.     $pop     eax
  798.     $add     al, '0'
  799.     $stosb
  800.     $jnz     F3
  801.    
  802.     $mov     al, '\0'
  803.     $stosb
  804.  
  805.     $popa
  806.     return EBX;
  807. }
  808.  
  809. inline dword memchr(dword s,int c,signed len)
  810. {
  811.         if(!len) return NULL;
  812.         do {
  813.                 if(DSBYTE[s] == c) return s;
  814.                 $inc s
  815.                 $dec len
  816.         } while(len);
  817.         return NULL;
  818. }
  819.  
  820. inline dword strdup(dword text)
  821. {
  822.     dword l = strlen(text);
  823.     dword ret = malloc(l+1);
  824.         if(!ret) return NULL;
  825.     strlcpy(ret,text,l);
  826.     return ret;
  827. }
  828.  
  829. inline dword strndup(dword str, signed maxlen)
  830. {
  831.         dword copy,len;
  832.  
  833.         len = strnlen(str, maxlen);
  834.         copy = malloc(len + 1);
  835.         if (copy != NULL)
  836.         {
  837.                 strlcpy(copy, str, len);
  838.                 DSBYTE[len+copy] = '\0';
  839.         }
  840.         return copy;
  841. }
  842.  
  843. inline dword hexdec(dword text)
  844. {
  845.         char s;
  846.         dword ret,l;
  847.         ret = 0;
  848.         s = DSBYTE[text];
  849.         while(s)
  850.         {      
  851.                 ret <<= 4;
  852.                 if(s>='A')&&(s<='F')ret |= s-'A'+10;
  853.                 else if(s>='a')&&(s<='f')ret |= s-'a'+10;
  854.                 else if(s>='0')&&(s<='9')ret |= s-'0';
  855.                 text++;
  856.                 s = DSBYTE[text];
  857.         }
  858.         return ret;
  859. }
  860.  
  861. inline signed csshexdec(dword text)
  862. {
  863.         char s;
  864.         dword ret,l;
  865.         byte tmp;
  866.         l = strlen(text);
  867.         ret = 0;
  868.         s = DSBYTE[text];
  869.         tmp = 0;
  870.         if(l==6) while(s)
  871.         {      
  872.                 ret <<= 4;
  873.                 if(s>='A')&&(s<='F')ret |= s-'A'+10;
  874.                 else if(s>='a')&&(s<='f')ret |= s-'a'+10;
  875.                 else if(s>='0')&&(s<='9')ret |= s-'0';
  876.                 text++;
  877.                 s = DSBYTE[text];
  878.         }
  879.         else if(l==3) while(s)
  880.         {      
  881.                 ret |= tmp;
  882.                 ret <<= 4;
  883.                 ret |= tmp;
  884.                 ret <<= 4;
  885.                 if(s>='A')&&(s<='F')tmp = s-'A'+10;
  886.                 else if(s>='a')&&(s<='f')tmp = s-'a'+10;
  887.                 else if(s>='0')&&(s<='9')tmp = s-'0';
  888.                 text++;
  889.                 s = DSBYTE[text];
  890.         }
  891.         return ret;
  892. }
  893.  
  894. void miniprintf(dword dst, format, insert_line)
  895. {
  896.         dword in_pos = strchr(format, '%');
  897.         if (ESBYTE[in_pos+1] == 's') {
  898.                 strlcpy(dst, format, in_pos - format);
  899.                 strcat(dst, insert_line);
  900.                 strcat(dst, in_pos+2);
  901.         }
  902. }
  903.  
  904. inline cdecl int sprintf(dword buf, format,...)
  905. {
  906.         #define END_ARGS 0xFF00FF //ARGS FUNCTION
  907.         byte s;
  908.         char X[10];
  909.         dword ret, tmp, l;
  910.         dword arg = #format;
  911.         ret = buf;
  912.         s = DSBYTE[format];
  913.         while(s){
  914.                 if(s=='%'){
  915.                         arg+=4;
  916.                         tmp = DSDWORD[arg];
  917.                         if(tmp==END_ARGS)goto END_FUNC_SPRINTF;
  918.                         $inc format
  919.                         s = DSBYTE[format];
  920.                         if(!s)goto END_FUNC_SPRINTF;
  921.                         switch(s)
  922.                         {
  923.                                 case 's':
  924.                                         l = tmp;
  925.                                         s = DSBYTE[tmp];
  926.                                         while(s)
  927.                                         {
  928.                                                 DSBYTE[buf] = s;
  929.                                                 $inc tmp
  930.                                                 $inc buf
  931.                                                 s = DSBYTE[tmp];
  932.                                         }
  933.                                 break;
  934.                                 case 'c':
  935.                                         DSBYTE[buf] = tmp;
  936.                                         $inc buf
  937.                                 break;
  938.                                 case 'u': //if(tmp<0)return ret;
  939.                                 case 'd':
  940.                                 case 'i':
  941.                                         tmp = itoa(tmp);
  942.                                         if(!DSBYTE[tmp])goto END_FUNC_SPRINTF;
  943.                                         l = strlen(tmp);
  944.                                         strlcpy(buf,tmp,l);
  945.                                         buf += l;
  946.                                 break;
  947.                                 case 'a':
  948.                                 case 'A':
  949.                                         strlcpy(buf,"0x00000000",10);
  950.                                         buf+=10;
  951.                                         l=buf;
  952.                                         while(tmp)
  953.                                         {
  954.                                                 $dec buf
  955.                                                 s=tmp&0xF;
  956.                                                 if(s>9)DSBYTE[buf]='A'+s-10;
  957.                                                 else DSBYTE[buf]='0'+s;
  958.                                                 tmp>>=4;
  959.                                         }
  960.                                         buf=l;
  961.                                 break;
  962.                                 case 'p':
  963.                                         tmp = itoa(#tmp);
  964.                                         if(!DSBYTE[tmp])goto END_FUNC_SPRINTF;
  965.                                         l = strlen(tmp);
  966.                                         strlcpy(buf,tmp,l);
  967.                                         buf += l;
  968.                                 break;
  969.                                 case '%':
  970.                                         DSBYTE[buf] = '%';
  971.                                         $inc buf
  972.                                 break;
  973.                                 default:
  974.                                 goto END_FUNC_SPRINTF;
  975.                         }
  976.                 }
  977.                 else {
  978.                         DSBYTE[buf] = s;
  979.                         $inc buf
  980.                 }
  981.                 $inc format
  982.                 s = DSBYTE[format];
  983.         }
  984.         END_FUNC_SPRINTF:
  985.         DSBYTE[buf] = 0;
  986.         return ret;
  987. }
  988.  
  989. inline signed strcoll(dword text1,text2)
  990. {
  991.         char s,ss;
  992.         loop()
  993.         {
  994.                 s = DSBYTE[text2];
  995.                 ss=strchr(text1,s);
  996.                 if(ss)return ss;
  997.                 text2++;
  998.         }
  999.         return 0;
  1000. }
  1001.  
  1002. // void * memset( ptr, value, num );
  1003. // fills the memory with a dword
  1004. // example: memset(str,'-', sizeof(str));
  1005. inline void MEMSETD(EDI,ECX,EAX)
  1006. {
  1007.         $REP
  1008.         $STOSD
  1009. }
  1010.  
  1011. :replace_char(dword in_str, char from_char, to_char, int length) {
  1012.         dword max = in_str + length;
  1013.         while (in_str < max) {
  1014.                 if (ESBYTE[in_str] == from_char) ESBYTE[in_str] = to_char;
  1015.                 in_str++;
  1016.         }
  1017. }
  1018.  
  1019. #endif