Subversion Repositories Kolibri OS

Rev

Rev 7863 | Rev 7941 | 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 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.         return true;
  224. }
  225.  
  226. inline fastcall void strcpy( EDI, ESI)
  227. {
  228.     $cld
  229. L2:
  230.     $lodsb
  231.     $stosb
  232.     $test al,al
  233.     $jnz L2
  234. }
  235.  
  236. inline fastcall int strlcpy(dword ESI, EDI, EBX)
  237. {
  238.     if (EBX<0) return -1;
  239.     EDX=0;
  240.     do {
  241.         DSBYTE[ESI]=DSBYTE[EDI];
  242.         ESI++;
  243.         EDI++;
  244.         EDX++;
  245.         if (EDX==EBX) { DSBYTE[ESI]='\0'; return -1;}
  246.     } while(DSBYTE[EDI-1]!='\0');
  247.     return 0;
  248. }
  249.  
  250. :void strncpy(dword dst, src, len)
  251. {
  252.         while (len) && (ESBYTE[src])
  253.         {
  254.                 ESBYTE[dst] = ESBYTE[src];
  255.                 dst++;
  256.                 src++;
  257.                 len--;
  258.         }
  259.         ESBYTE[dst]=0;
  260. }
  261.  
  262. /*
  263. inline fastcall void strtrim( ESI)
  264. {
  265.     EDI = ESI;
  266.     do{
  267.         AL=DSBYTE[EDI];
  268.         if (AL != '\32') && (AL != '\13') && (AL != '\10')
  269.         {
  270.             DSBYTE[ESI]=AL;
  271.             $inc ESI
  272.         }
  273.          $inc EDI
  274.     }while(AL!=0);
  275.     DSBYTE[ESI] = '\0';
  276. }
  277. */
  278.  
  279. inline byte __isWhite(int s){ if (s==13)||(s==32)||(s==10)||(s==9) return true; return false; }
  280. inline void strltrim(dword text){
  281.         int s;
  282.         dword back_text;
  283.         back_text = text;
  284.         s = ESBYTE[text];
  285.         while(__isWhite(s))
  286.         {
  287.                 $inc text
  288.                 s = ESBYTE[text];
  289.         }
  290.         loop()
  291.         {
  292.                 ESBYTE[back_text] = s;
  293.                 $inc back_text
  294.                 if(!s) break;
  295.                 $inc text
  296.                 s = ESBYTE[text];
  297.         };
  298. }
  299.  
  300. inline void strrtrim(dword text)
  301. {
  302.         int s;
  303.         dword p;
  304.         do {
  305.                 s = ESBYTE[text];
  306.                 if(__isWhite(s))
  307.                 {
  308.                         p = text;
  309.                         while(__isWhite(s))
  310.                         {
  311.                                 $inc text;
  312.                                 s = ESBYTE[text];
  313.                         }
  314.                 }
  315.                 else $inc text
  316.         } while(s);
  317.         $dec text
  318.         s = ESBYTE[text];
  319.         if(__isWhite(s)) ESBYTE[p] = 0;
  320. }
  321.  
  322. inline void strtrim(dword text){
  323.         int s;
  324.         dword p,back_text;
  325.         back_text = text;
  326.         s = ESBYTE[text];
  327.         while(__isWhite(s))
  328.         {
  329.                 $inc text
  330.                 s = ESBYTE[text];
  331.         }
  332.         do {
  333.                 s = ESBYTE[text];
  334.                 if(__isWhite(s))
  335.                 {
  336.                         p = back_text;
  337.                         while(__isWhite(s))
  338.                         {
  339.                                 ESBYTE[back_text] = s;
  340.                                 $inc back_text
  341.                                 $inc text;
  342.                                 s = ESBYTE[text];
  343.                         }
  344.                 }
  345.                 else {
  346.                         ESBYTE[back_text] = s;
  347.                         $inc back_text
  348.                         $inc text
  349.                 }
  350.         } while(s);
  351.         $dec text
  352.         s = ESBYTE[text];
  353.         if(__isWhite(s)) ESBYTE[p] = 0;
  354. }
  355.  
  356. inline fastcall void strcat( EDI, ESI)
  357. {
  358.   asm {
  359.     mov ebx, edi
  360.     xor ecx, ecx
  361.     xor eax, eax
  362.     dec ecx
  363.     repne scasb
  364.     dec edi
  365.     mov edx, edi
  366.     mov edi, esi
  367.     xor ecx, ecx
  368.     xor eax, eax
  369.     dec ecx
  370.     repne scasb
  371.     xor ecx, 0ffffffffh
  372.     mov edi, edx
  373.     mov edx, ecx
  374.     mov eax, edi
  375.     shr ecx, 2
  376.     rep movsd
  377.     mov ecx, edx
  378.     and ecx, 3
  379.     rep movsb
  380.     mov eax, ebx
  381.     }
  382. }
  383.  
  384. :void strncat(dword dst, src, len)
  385. {
  386.         while (ESBYTE[dst]) && (len) {
  387.                 dst++;
  388.                 len--;
  389.         }
  390.         while (ESBYTE[src]) && (len>1) {
  391.                 ESBYTE[dst] = ESBYTE[src];
  392.                 dst++;
  393.                 src++;
  394.                 len--;
  395.         }
  396.         ESBYTE[dst] = 0;
  397. }
  398.  
  399. :void chrncat(dword dst, unsigned char s, dword len)
  400. {
  401.         while (ESBYTE[dst]) && (len) {
  402.                 dst++;
  403.                 len--;
  404.         }
  405.         if (len>1) {
  406.                 ESBYTE[dst] = s;
  407.                 ESBYTE[dst+1] = 0;
  408.         }
  409. }
  410.  
  411. inline fastcall void chrcat(ESI, DI)
  412. {
  413.     while (ESBYTE[ESI]) ESI++;
  414.     ESBYTE[ESI] = DI;
  415.     ESI++;
  416.     ESBYTE[ESI] = 0;
  417. }
  418.  
  419. inline dword strchr(dword shb;char s)
  420. {
  421.         char ss;
  422.         loop()
  423.         {
  424.                 ss = DSBYTE[shb];
  425.                 if(!ss)return 0;
  426.                 if(ss==s)return shb;
  427.                 shb++;
  428.         }
  429. }
  430.  
  431. inline fastcall signed int strrchr( ESI,BL)
  432. {
  433.     int jj=0, last=0;
  434.     do{
  435.         jj++;
  436.         $lodsb
  437.         IF(AL==BL) last=jj;
  438.     } while(AL!=0);
  439.     return last;
  440. }
  441.  
  442.  
  443. inline fastcall unsigned int chrnum( ESI, BL)
  444. {
  445.     int num = 0;
  446.     while(DSBYTE[ESI])
  447.     {
  448.         if (DSBYTE[ESI] == BL) num++;
  449.         ESI++;
  450.     }
  451.     return num;
  452. }
  453.  
  454.  
  455. inline fastcall signed int strstr( EBX, EDX)
  456. {
  457.   asm {
  458.     MOV EDI, EDX
  459.     XOR ECX, ECX
  460.     XOR EAX, EAX
  461.     DEC ECX
  462.     REPNE SCASB
  463.     NOT ECX
  464.     DEC ECX
  465.     JE LS2
  466.     MOV ESI, ECX
  467.     XOR ECX, ECX
  468.     MOV EDI, EBX
  469.     DEC ECX
  470.     REPNE SCASB
  471.     NOT ECX
  472.     SUB ECX, ESI
  473.     JBE LS2
  474.     MOV EDI, EBX
  475.     LEA EBX, DSDWORD[ ESI-1]
  476. LS1: MOV ESI, EDX
  477.     LODSB
  478.     REPNE SCASB
  479.     JNE LS2
  480.     MOV EAX, ECX
  481.     PUSH EDI
  482.     MOV ECX, EBX
  483.     REPE CMPSB
  484.     POP EDI
  485.     MOV ECX, EAX
  486.     JNE LS1
  487.     LEA EAX, DSDWORD[ EDI-1]
  488.     JMP SHORT LS3
  489. LS2: XOR EAX, EAX
  490. LS3:
  491.   }
  492. }
  493.  
  494. inline signed int strcmpi(dword cmp1, cmp2)
  495. {
  496.     char si, ue;
  497.  
  498.     loop()
  499.     {
  500.         si = DSBYTE[cmp1];
  501.         ue = DSBYTE[cmp2];
  502.         if (si>='A') && (si<='Z') si +=32;
  503.         if (ue>='A') && (ue<='Z') ue +=32;
  504.         if (si != ue) return si-ue;
  505.         cmp1++;
  506.         cmp2++;
  507.         if ((DSBYTE[cmp1]=='\0') && (DSBYTE[cmp2]=='\0')) return 0;
  508.         if (DSBYTE[cmp1]=='\0') return -1;
  509.         if (DSBYTE[cmp2]=='\0') return 1;
  510.     }
  511. }
  512.  
  513. inline dword strstri(dword searchin, usestr_s)
  514. {
  515.     dword usestr_e = usestr_s;
  516.     char si, ue;
  517.  
  518.     while(DSBYTE[searchin])
  519.     {
  520.         si = DSBYTE[searchin];
  521.         ue = DSBYTE[usestr_e];
  522.         if (si>='A') && (si<='Z') si +=32;
  523.         if (ue>='A') && (ue<='Z') ue +=32;
  524.         if (si == ue) usestr_e++; else usestr_e = usestr_s;
  525.         searchin++;
  526.         if (DSBYTE[usestr_e]=='\0') return searchin;
  527.     }
  528.     return -1;
  529. }
  530.  
  531.  
  532. inline unsigned int strcpyb(dword search_in, copyin, startstr, endstr)
  533. {
  534.     dword startp, endp;
  535.     dword copyin_start_off = copyin;
  536.     if (startstr==0) startp = search_in; else startp = strstr(search_in, startstr) + strlen(startstr);
  537.     endp = strstri(startp, endstr);
  538.     if (endp==0) endp = startp+strlen(search_in);
  539.     //if (startp==endp) return 0;
  540.     do
  541.     {
  542.         DSBYTE[copyin] = DSBYTE[startp];
  543.         copyin++;
  544.         startp++;
  545.     }
  546.     while (startp<endp);
  547.     DSBYTE[copyin] = '\0';
  548.     return copyin_start_off;
  549. }
  550.  
  551.  
  552. /*void strcat(char *to, char *from)
  553. {
  554.     while(*to) to++;
  555.     while(*from)
  556.     {
  557.         *to = *from;
  558.         to++;
  559.         from++;
  560.     }
  561.     *to = '\0';
  562. }*/
  563.  
  564.  
  565. inline fastcall dword atoi( EDI)
  566. {
  567.     $push ebx
  568.     $push esi
  569.     ESI=EDI;
  570.     while (DSBYTE[ESI]==' ') ESI++;
  571.     if (DSBYTE[ESI]=='-') ESI++;
  572.     EAX=0;
  573.     while (DSBYTE[ESI]>='0') && (DSBYTE[ESI]<='9')
  574.     {
  575.         $xor ebx, ebx
  576.         EBX = DSBYTE[ESI]-'0';
  577.         EAX *= 10;
  578.         EAX += EBX;
  579.         ESI++;
  580.     }
  581.     IF (DSBYTE[EDI]=='-') -EAX;
  582.     $pop esi
  583.     $pop ebx
  584. }
  585.  
  586.  
  587.  
  588. inline fastcall strupr( ESI)
  589. {
  590.     do{
  591.         AL=DSBYTE[ESI];
  592.         IF(AL>='a')IF(AL<='z')DSBYTE[ESI]=AL&0x5f;
  593.         IF (AL>=160) && (AL<=175) DSBYTE[ESI] = AL - 32;    //à-ï
  594.         IF (AL>=224) && (AL<=239) DSBYTE[ESI] = AL - 80;    //à-ï
  595.          ESI++;
  596.     }while(AL!=0);
  597. }
  598.  
  599. inline fastcall strlwr( ESI)
  600. {
  601.     do{
  602.         $LODSB
  603.         IF(AL>='A')&&(AL<='Z'){
  604.             AL+=0x20;
  605.             DSBYTE[ESI-1]=AL;
  606.             CONTINUE;
  607.         }
  608.     }while(AL!=0);
  609. }
  610.  
  611. inline fastcall strttl( EDX)
  612. {
  613.     AL=DSBYTE[EDX];
  614.     IF(AL>='a')&&(AL<='z')DSBYTE[EDX]=AL&0x5f;
  615.     IF (AL>=160) && (AL<=175) DSBYTE[EDX] = AL - 32;    //à-ï
  616.     IF (AL>=224) && (AL<=239) DSBYTE[EDX] = AL - 80;    //à-ï
  617.     do{
  618.         EDX++;
  619.         AL=DSBYTE[EDX];
  620.         IF(AL>='A')&&(AL<='Z'){DSBYTE[EDX]=AL|0x20; CONTINUE;}
  621.         IF(AL>='€')&&(AL<='')DSBYTE[EDX]=AL|0x20; // -¯
  622.         IF (AL>=144) && (AL<=159) DSBYTE[EDX] = AL + 80;    //à-ï
  623.     }while(AL!=0);
  624. }
  625.  
  626. /*
  627. dword itoa( ESI)
  628. {
  629.     unsigned char buffer[11];
  630.     $pusha
  631.     EDI = #buffer;
  632.     ECX = 10;
  633.     if (ESI < 0)
  634.     {
  635.          $mov     al, '-'
  636.          $stosb
  637.          $neg     esi
  638.     }
  639.  
  640.     $mov     eax, esi
  641.     $push    -'0'
  642. F2:
  643.     $xor     edx, edx
  644.     $div     ecx
  645.     $push    edx
  646.     $test    eax, eax
  647.     $jnz     F2
  648. F3:
  649.     $pop     eax
  650.     $add     al, '0'
  651.     $stosb
  652.     $jnz     F3
  653.    
  654.     $mov     al, '\0'
  655.     $stosb
  656.  
  657.     $popa
  658.     return #buffer;
  659. }
  660. */
  661. :unsigned char BUF_ITOA[11];
  662. inline dword itoa(signed long number)
  663. {
  664.         dword ret,p;
  665.         byte cmd;
  666.         long mask,tmp;
  667.         mask = 1000000000;
  668.         cmd = true;
  669.         p = #BUF_ITOA;
  670.         if(!number){
  671.                 ESBYTE[p] = '0';
  672.                 ESBYTE[p+1] = 0;
  673.                 return p;
  674.         }
  675.         ret = p;
  676.         if(number<0)
  677.         {
  678.                 $neg number
  679.                 ESBYTE[p] = '-';
  680.                 $inc p
  681.         }
  682.         while(mask)
  683.         {
  684.                 tmp = number / mask;
  685.                 tmp = tmp%10;
  686.                
  687.                 if(cmd){
  688.                         if(tmp){
  689.                                 ESBYTE[p] = tmp + '0';
  690.                                 $inc p
  691.                                 cmd = false;
  692.                         }
  693.                 }
  694.                 else {
  695.                         ESBYTE[p] = tmp + '0';
  696.                         $inc p
  697.                 }
  698.                 mask /= 10;
  699.         }
  700.         ESBYTE[p] = 0;
  701.         return ret;
  702. }
  703.  
  704. inline fastcall itoa_(signed int EDI, ESI)
  705. {
  706.     $pusha
  707.     EBX = EDI;
  708.     ECX = 10;
  709.     if (ESI > 90073741824)
  710.     {
  711.          $mov     al, '-'
  712.          $stosb
  713.          $neg     esi
  714.     }
  715.  
  716.     $mov     eax, esi
  717.     $push    -'0'
  718. F2:
  719.     $xor     edx, edx
  720.     $div     ecx
  721.     $push    edx
  722.     $test    eax, eax
  723.     $jnz     F2
  724. F3:
  725.     $pop     eax
  726.     $add     al, '0'
  727.     $stosb
  728.     $jnz     F3
  729.    
  730.     $mov     al, '\0'
  731.     $stosb
  732.  
  733.     $popa
  734.     return EBX;
  735. }
  736.  
  737. inline dword memchr(dword s,int c,signed len)
  738. {
  739.         if(!len) return NULL;
  740.         do {
  741.                 if(DSBYTE[s] == c) return s;
  742.                 $inc s
  743.                 $dec len
  744.         } while(len);
  745.         return NULL;
  746. }
  747.  
  748. inline dword strdup(dword text)
  749. {
  750.     dword l = strlen(text);
  751.     dword ret = malloc(l+1);
  752.         if(!ret) return NULL;
  753.     strlcpy(ret,text,l);
  754.     return ret;
  755. }
  756.  
  757. inline dword strndup(dword str, signed maxlen)
  758. {
  759.         dword copy,len;
  760.  
  761.         len = strnlen(str, maxlen);
  762.         copy = malloc(len + 1);
  763.         if (copy != NULL)
  764.         {
  765.                 strlcpy(copy, str, len);
  766.                 DSBYTE[len+copy] = '\0';
  767.         }
  768.         return copy;
  769. }
  770.  
  771. inline dword hexdec(dword text)
  772. {
  773.         char s;
  774.         dword ret,l;
  775.         ret = 0;
  776.         s = DSBYTE[text];
  777.         while(s)
  778.         {      
  779.                 ret <<= 4;
  780.                 if(s>='A')&&(s<='F')ret |= s-'A'+10;
  781.                 else if(s>='a')&&(s<='f')ret |= s-'a'+10;
  782.                 else if(s>='0')&&(s<='9')ret |= s-'0';
  783.                 text++;
  784.                 s = DSBYTE[text];
  785.         }
  786.         return ret;
  787. }
  788.  
  789. inline signed csshexdec(dword text)
  790. {
  791.         char s;
  792.         dword ret,l;
  793.         byte tmp;
  794.         l = strlen(text);
  795.         ret = 0;
  796.         s = DSBYTE[text];
  797.         tmp = 0;
  798.         if(l==6) while(s)
  799.         {      
  800.                 ret <<= 4;
  801.                 if(s>='A')&&(s<='F')ret |= s-'A'+10;
  802.                 else if(s>='a')&&(s<='f')ret |= s-'a'+10;
  803.                 else if(s>='0')&&(s<='9')ret |= s-'0';
  804.                 text++;
  805.                 s = DSBYTE[text];
  806.         }
  807.         else if(l==3) while(s)
  808.         {      
  809.                 ret |= tmp;
  810.                 ret <<= 4;
  811.                 ret |= tmp;
  812.                 ret <<= 4;
  813.                 if(s>='A')&&(s<='F')tmp = s-'A'+10;
  814.                 else if(s>='a')&&(s<='f')tmp = s-'a'+10;
  815.                 else if(s>='0')&&(s<='9')tmp = s-'0';
  816.                 text++;
  817.                 s = DSBYTE[text];
  818.         }
  819.         return ret;
  820. }
  821.  
  822. inline cdecl int sprintf(dword buf, format,...)
  823. {
  824.         #define END_ARGS 0xFF00FF //ARGS FUNCTION
  825.         byte s;
  826.         char X[10];
  827.         dword ret, tmp, l;
  828.         dword arg = #format;
  829.         ret = buf;
  830.         s = DSBYTE[format];
  831.         while(s){
  832.                 if(s=='%'){
  833.                         arg+=4;
  834.                         tmp = DSDWORD[arg];
  835.                         if(tmp==END_ARGS)goto END_FUNC_SPRINTF;
  836.                         $inc format
  837.                         s = DSBYTE[format];
  838.                         if(!s)goto END_FUNC_SPRINTF;
  839.                         switch(s)
  840.                         {
  841.                                 case 's':
  842.                                         l = tmp;
  843.                                         s = DSBYTE[tmp];
  844.                                         while(s)
  845.                                         {
  846.                                                 DSBYTE[buf] = s;
  847.                                                 $inc tmp
  848.                                                 $inc buf
  849.                                                 s = DSBYTE[tmp];
  850.                                         }
  851.                                 break;
  852.                                 case 'c':
  853.                                         DSBYTE[buf] = tmp;
  854.                                         $inc buf
  855.                                 break;
  856.                                 case 'u': //if(tmp<0)return ret;
  857.                                 case 'd':
  858.                                 case 'i':
  859.                                         tmp = itoa(tmp);
  860.                                         if(!DSBYTE[tmp])goto END_FUNC_SPRINTF;
  861.                                         l = strlen(tmp);
  862.                                         strlcpy(buf,tmp,l);
  863.                                         buf += l;
  864.                                 break;
  865.                                 case 'a':
  866.                                 case 'A':
  867.                                         strlcpy(buf,"0x00000000",10);
  868.                                         buf+=10;
  869.                                         l=buf;
  870.                                         while(tmp)
  871.                                         {
  872.                                                 $dec buf
  873.                                                 s=tmp&0xF;
  874.                                                 if(s>9)DSBYTE[buf]='A'+s-10;
  875.                                                 else DSBYTE[buf]='0'+s;
  876.                                                 tmp>>=4;
  877.                                         }
  878.                                         buf=l;
  879.                                 break;
  880.                                 case 'p':
  881.                                         tmp = itoa(#tmp);
  882.                                         if(!DSBYTE[tmp])goto END_FUNC_SPRINTF;
  883.                                         l = strlen(tmp);
  884.                                         strlcpy(buf,tmp,l);
  885.                                         buf += l;
  886.                                 break;
  887.                                 case '%':
  888.                                         DSBYTE[buf] = '%';
  889.                                         $inc buf
  890.                                 break;
  891.                                 default:
  892.                                 goto END_FUNC_SPRINTF;
  893.                         }
  894.                 }
  895.                 else {
  896.                         DSBYTE[buf] = s;
  897.                         $inc buf
  898.                 }
  899.                 $inc format
  900.                 s = DSBYTE[format];
  901.         }
  902.         END_FUNC_SPRINTF:
  903.         DSBYTE[buf] = 0;
  904.         return ret;
  905. }
  906.  
  907. inline signed strcoll(dword text1,text2)
  908. {
  909.         char s,ss;
  910.         loop()
  911.         {
  912.                 s = DSBYTE[text2];
  913.                 ss=strchr(text1,s);
  914.                 if(ss)return ss;
  915.                 text2++;
  916.         }
  917.         return 0;
  918. }
  919.  
  920. // void * memset( ptr, value, num );
  921. // fills the memory with a dword
  922. // example: memset(str,'-', sizeof(str));
  923. inline void MEMSETD(EDI,ECX,EAX)
  924. {
  925.         $REP
  926.         $STOSD
  927. }
  928.  
  929. :replace_char(dword in_str, char from_char, to_char, int length) {
  930.         dword max = in_str + length;
  931.         while (in_str < max) {
  932.                 if (ESBYTE[in_str] == from_char) ESBYTE[in_str] = to_char;
  933.                 in_str++;
  934.         }
  935. }
  936.  
  937. #endif