Subversion Repositories Kolibri OS

Rev

Rev 5574 | Rev 5576 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. //------------------------------------------------------------------------------
  2. // strspn(dword text1,text2) --- example: strspn("12 year","1234567890") -> return 2
  3. // strpbrk(dword text1,text2) --- example: strpbrk("this test", " ckfi") -> return "is test"
  4. // strcmp( ESI, EDI)
  5. // strlen( EDI)
  6. // strcpy( EDI, ESI) --- 0 if ==
  7. // strncpy(dword text1,text2,signed length)
  8. // strcat( EDI, ESI)
  9. // strncat(dword text1,text2,signed length) --- pasting the text of a certain length
  10. // strchr( ESI,BL) --- find first BL
  11. // strrchr( ESI,BL) --- find last BL
  12. // strstr( EBX, EDX)
  13. // itoa(signed long number) --- convert the number as a string
  14. // atoi(dword text) --- convert a string as a number
  15. // strupr( ESI)
  16. // strlwr( ESI) --- kyrillic symbols may not work
  17. // strttl( EDX)
  18. // strtok( ESI)
  19. // strltrim(dword text) --- removes "blank" characters on the left (\r, \n and space)
  20. // strrtrim(dword text) --- removes "blank" characters on the right (\r, \n and space)
  21. // strtrim(dword text) --- delete "empty" characters (\ r \ n and space) on both sides
  22. // chrnum(dword searchin, char symbol)
  23. // strcpyb(dword searchin, copyin, startstr, endstr) --- copy string between strings
  24. // strnumb(dword searchin, startstr, endstr) --- get number between strings
  25. // strdup(dword text) --- allocation under the text
  26. //------------------------------------------------------------------------------
  27.  
  28. /*
  29. inline fastcall signed int strcmp( ESI, EDI)
  30. {
  31.     loop()
  32.     {
  33.         IF (DSBYTE[ESI]<DSBYTE[EDI]) RETURN -1;
  34.         IF (DSBYTE[ESI]>DSBYTE[EDI]) RETURN 1;
  35.         IF (DSBYTE[ESI]=='\0') RETURN 0;
  36.         ESI++;
  37.         EDI++;
  38.     }
  39. }
  40. */
  41.  
  42. inline int strspn(dword text1,text2)
  43. {
  44.         dword beg;
  45.         char s1,s2;
  46.         int ret;
  47.         ret = 0;
  48.         beg = text2;
  49.         do {
  50.                 s1 = ESBYTE[text1];
  51.                 text2 = beg;
  52.                 do {
  53.                         s2 = ESBYTE[text2];
  54.                         if(s1==s2)
  55.                         {
  56.                                 if(!s2)break;
  57.                                 $inc ret
  58.                                 break;
  59.                         }
  60.                         else $inc text2
  61.                 } while(s2);
  62.                 $inc text1
  63.         } while(s1);
  64.         return ret;
  65. }
  66.  
  67. inline dword strpbrk(dword text1,text2)
  68. {
  69.         char s,ss;
  70.         dword beg;
  71.         beg = text2;
  72.         do {
  73.                 s = ESBYTE[text1];
  74.                 text2 = beg;
  75.                 do {
  76.                         ss = ESBYTE[text2];
  77.                         if(ss==s) return text1;
  78.                         $inc text2
  79.                 } while(ss);
  80.                 $inc text1
  81.         } while(s);
  82.         return text1;
  83. }
  84.  
  85. inline fastcall signed int strncmp( ESI, EDI, ECX)
  86. {
  87.   asm {
  88.     MOV EBX, EDI
  89.     XOR EAX, EAX
  90.     MOV EDX, ECX
  91.     OR ECX, ECX
  92.     JE L1
  93.     REPNE SCASB
  94.     SUB EDX, ECX
  95.     MOV ECX, EDX
  96.     MOV EDI, EBX
  97.     XOR EBX, EBX
  98.     REPE CMPSB
  99.     MOV AL, DSBYTE[ ESI-1]
  100.     MOV BL, DSBYTE[ EDI-1]
  101.     SUB EAX, EBX
  102. L1:
  103.   }
  104. }
  105.  
  106.  
  107. inline fastcall unsigned int strlen( EDI)
  108. {
  109.     $xor eax, eax
  110.     $mov ecx, -1
  111.     $REPNE $SCASB
  112.     EAX-=2+ECX;
  113. }
  114.  
  115. inline strnlen(dword str, dword maxlen)
  116. {
  117.         dword cp;
  118.         for (cp = str; (maxlen != 0) && (DSBYTE[cp] != '\0'); cp++, maxlen--);
  119.         return cp - str;
  120. }
  121.  
  122.  
  123. inline signed int strcmp(dword text1, text2)
  124. {
  125.         char s1,s2;
  126.         dword p1,p2;
  127.         p1 = text1;
  128.         p2 = text2;
  129.         loop()
  130.         {
  131.                 s1 = DSBYTE[text1];
  132.                 s2 = DSBYTE[text2];
  133.                 if(s1==s2)
  134.                 {
  135.                         if(s1==0) return 0;
  136.                 }
  137.                 else {
  138.                         return s1-s2;
  139.                 }
  140.                 $inc text1
  141.                 $inc text2
  142.         }
  143.         return 0;
  144. }
  145.  
  146. /*
  147. signed int strncmp(dword s1, s2, signed n)
  148. unsigned char _s1,_s2;
  149. {
  150.         if (n == 0)
  151.                 return 0;
  152.         do {
  153.                 _s1 = DSBYTE[s1];
  154.                 _s2 = DSBYTE[s2];
  155.                 if (_s1 != _s2)
  156.                 {
  157.                         $dec s2
  158.                         return _s1 - _s2;
  159.                 }
  160.                 $inc s2
  161.                 if (_s1 == 0)
  162.                         break;
  163.                 $inc s1
  164.                 $dec n
  165.         } while (n);
  166.         return 0;
  167. }
  168. */
  169.  
  170.  
  171. inline fastcall void strcpy( EDI, ESI)
  172. {
  173.     $cld
  174. L2:
  175.     $lodsb
  176.     $stosb
  177.     $test al,al
  178.     $jnz L2
  179. }
  180.  
  181. inline dword strncpy(dword text1, text2, signed len)
  182.         signed o1,o2;
  183. {
  184.         o1 = len/4;
  185.         o2 = len-4*o1;
  186.         while(o1){
  187.                 ESDWORD[text1] = ESDWORD[text2];
  188.                 text1 += 4;
  189.                 text2 += 4;
  190.                 $dec o1
  191.         }
  192.         while(o2){
  193.                 ESBYTE[text1] = ESBYTE[text2];
  194.                 $inc text1
  195.                 $inc text2
  196.                 $dec o2
  197.         }
  198.         ESBYTE[text1] = 0;
  199.         return text1;
  200. }
  201.  
  202. inline fastcall int strlcpy(dword ESI, EDI, EBX)
  203. {
  204.     if (EBX<0) return -1;
  205.     EDX=0;
  206.     do {
  207.         DSBYTE[ESI]=DSBYTE[EDI];
  208.         ESI++;
  209.         EDI++;
  210.         EDX++;
  211.         if (EDX==EBX) { DSBYTE[ESI]='\0'; return -1;}
  212.     } while(DSBYTE[EDI-1]!='\0');
  213.     return 0;
  214. }
  215.  
  216. /*
  217. inline fastcall void strtrim( ESI)
  218. {
  219.     EDI = ESI;
  220.     do{
  221.         AL=DSBYTE[EDI];
  222.         if (AL != '\32') && (AL != '\13') && (AL != '\10')
  223.         {
  224.             DSBYTE[ESI]=AL;
  225.             $inc ESI
  226.         }
  227.          $inc EDI
  228.     }while(AL!=0);
  229.     DSBYTE[ESI] = '\0';
  230. }
  231. */
  232.  
  233. byte __isWhite(int s){ if (s==13)||(s==32)||(s==10)||(s==9) return true; return false; }
  234. inline void strltrim(dword text){
  235.         int s;
  236.         dword back_text;
  237.         back_text = text;
  238.         s = ESBYTE[text];
  239.         while(__isWhite(s))
  240.         {
  241.                 $inc text
  242.                 s = ESBYTE[text];
  243.         }
  244.         loop()
  245.         {
  246.                 ESBYTE[back_text] = s;
  247.                 $inc back_text
  248.                 if(!s) break;
  249.                 $inc text
  250.                 s = ESBYTE[text];
  251.         };
  252. }
  253.  
  254. inline void strrtrim(dword text)
  255. {
  256.         int s;
  257.         dword p;
  258.         do {
  259.                 s = ESBYTE[text];
  260.                 if(__isWhite(s))
  261.                 {
  262.                         p = text;
  263.                         while(__isWhite(s))
  264.                         {
  265.                                 $inc text;
  266.                                 s = ESBYTE[text];
  267.                         }
  268.                 }
  269.                 else $inc text
  270.         } while(s);
  271.         $dec text
  272.         s = ESBYTE[text];
  273.         if(__isWhite(s)) ESBYTE[p] = 0;
  274. }
  275.  
  276. inline void strtrim(dword text){
  277.         int s;
  278.         dword p,back_text;
  279.         back_text = text;
  280.         s = ESBYTE[text];
  281.         while(__isWhite(s))
  282.         {
  283.                 $inc text
  284.                 s = ESBYTE[text];
  285.         }
  286.         do {
  287.                 s = ESBYTE[text];
  288.                 if(__isWhite(s))
  289.                 {
  290.                         p = back_text;
  291.                         while(__isWhite(s))
  292.                         {
  293.                                 ESBYTE[back_text] = s;
  294.                                 $inc back_text
  295.                                 $inc text;
  296.                                 s = ESBYTE[text];
  297.                         }
  298.                 }
  299.                 else {
  300.                         ESBYTE[back_text] = s;
  301.                         $inc back_text
  302.                         $inc text
  303.                 }
  304.         } while(s);
  305.         $dec text
  306.         s = ESBYTE[text];
  307.         if(__isWhite(s)) ESBYTE[p] = 0;
  308. }
  309.  
  310. inline fastcall void strcat( EDI, ESI)
  311. {
  312.   asm {
  313.     mov ebx, edi
  314.     xor ecx, ecx
  315.     xor eax, eax
  316.     dec ecx
  317.     repne scasb
  318.     dec edi
  319.     mov edx, edi
  320.     mov edi, esi
  321.     xor ecx, ecx
  322.     xor eax, eax
  323.     dec ecx
  324.     repne scasb
  325.     xor ecx, 0ffffffffh
  326.     mov edi, edx
  327.     mov edx, ecx
  328.     mov eax, edi
  329.     shr ecx, 2
  330.     rep movsd
  331.     mov ecx, edx
  332.     and ecx, 3
  333.     rep movsb
  334.     mov eax, ebx
  335.     }
  336. }
  337.  
  338. void strncat(dword text1, text2, signed len)
  339.         signed o1,o2;
  340.         char s;
  341. {
  342.         s = ESBYTE[text1];
  343.         while(s){
  344.                 $inc text1
  345.                 s = ESBYTE[text1];
  346.         }
  347.         o1 = len/4;
  348.         o2 = len-4*o1;
  349.         while(o1){
  350.                 ESDWORD[text1] = ESDWORD[text2];
  351.                 text1 += 4;
  352.                 text2 += 4;
  353.                 $dec o1
  354.         }
  355.         while(o2){
  356.                 ESBYTE[text1] = ESBYTE[text2];
  357.                 $inc text1
  358.                 $inc text2
  359.                 $dec o2
  360.         }
  361. }
  362.  
  363. inline fastcall void chrcat(ESI, BL)
  364. {
  365.     EDI = strlen(ESI);
  366.     ESBYTE[ESI+EDI] = BL;
  367.     ESBYTE[ESI+EDI+1] = 0;
  368. }
  369.  
  370.  
  371. inline fastcall signed int strchr( ESI,BL)
  372. {
  373.     int jj=0;
  374.     do{
  375.         jj++;
  376.         $lodsb
  377.         IF(AL==BL) return jj;
  378.     } while(AL!=0);
  379.     return 0;
  380. }
  381.  
  382.  
  383. inline fastcall signed int strrchr( ESI,BL)
  384. {
  385.     int jj=0, last=0;
  386.     do{
  387.         jj++;
  388.         $lodsb
  389.         IF(AL==BL) last=jj;
  390.     } while(AL!=0);
  391.     return last;
  392. }
  393.  
  394.  
  395. int chrnum(dword searchin, char symbol)
  396. {
  397.     int num = 0;
  398.     while(DSBYTE[searchin])
  399.     {
  400.         if (DSBYTE[searchin] == symbol)    num++;
  401.         searchin++;
  402.     }
  403.     return num;
  404. }
  405.  
  406.  
  407. inline fastcall signed int strstr( EBX, EDX)
  408. {
  409.   asm {
  410.     MOV EDI, EDX
  411.     XOR ECX, ECX
  412.     XOR EAX, EAX
  413.     DEC ECX
  414.     REPNE SCASB
  415.     NOT ECX
  416.     DEC ECX
  417.     JE LS2
  418.     MOV ESI, ECX
  419.     XOR ECX, ECX
  420.     MOV EDI, EBX
  421.     DEC ECX
  422.     REPNE SCASB
  423.     NOT ECX
  424.     SUB ECX, ESI
  425.     JBE LS2
  426.     MOV EDI, EBX
  427.     LEA EBX, DSDWORD[ ESI-1]
  428. LS1: MOV ESI, EDX
  429.     LODSB
  430.     REPNE SCASB
  431.     JNE LS2
  432.     MOV EAX, ECX
  433.     PUSH EDI
  434.     MOV ECX, EBX
  435.     REPE CMPSB
  436.     POP EDI
  437.     MOV ECX, EAX
  438.     JNE LS1
  439.     LEA EAX, DSDWORD[ EDI-1]
  440.     JMP SHORT LS3
  441. LS2: XOR EAX, EAX
  442. LS3:
  443.   }
  444. }
  445.  
  446. dword strcmpi(dword cmp1, cmp2)
  447. {
  448.     char si, ue;
  449.  
  450.     loop()
  451.     {
  452.         si = DSBYTE[cmp1];
  453.         ue = DSBYTE[cmp2];
  454.         if (si>='A') && (si<='Z') si +=32;
  455.         if (ue>='A') && (ue<='Z') ue +=32;
  456.         if (si != ue) return -1;
  457.         cmp1++;
  458.         cmp2++;
  459.         if ((DSBYTE[cmp1]=='\0') && (DSBYTE[cmp2]=='\0')) return 0;
  460.         if (DSBYTE[cmp1]=='\0') return -1;
  461.         if (DSBYTE[cmp2]=='\0') return 1;
  462.     }
  463. }
  464.  
  465. dword strstri(dword searchin, usestr_s)
  466. {
  467.     dword usestr_e = usestr_s;
  468.     char si, ue;
  469.  
  470.     while(DSBYTE[searchin])
  471.     {
  472.         si = DSBYTE[searchin];
  473.         ue = DSBYTE[usestr_e];
  474.         if (si>='A') && (si<='Z') si +=32;
  475.         if (ue>='A') && (ue<='Z') ue +=32;
  476.         if (si == ue) usestr_e++; else usestr_e = usestr_s;
  477.         searchin++;
  478.         if (DSBYTE[usestr_e]=='\0') return searchin;
  479.     }
  480.     return 0;
  481. }
  482.  
  483.  
  484. unsigned int strcpyb(dword search_in, copyin, startstr, endstr)
  485. {
  486.     dword startp, endp;
  487.     dword copyin_start_off = copyin;
  488.     if (startstr==0) startp = search_in; else startp = strstr(search_in, startstr) + strlen(startstr);
  489.     endp = strstri(startp, endstr);
  490.     if (endp==0) endp = startp+strlen(search_in);
  491.     //if (startp==endp) return 0;
  492.     do
  493.     {
  494.         DSBYTE[copyin] = DSBYTE[startp];
  495.         copyin++;
  496.         startp++;
  497.     }
  498.     while (startp<endp);
  499.     DSBYTE[copyin] = '\0';
  500.     return copyin_start_off;
  501. }
  502.  
  503.  
  504. /*void strcat(char *to, char *from)
  505. {
  506.     while(*to) to++;
  507.     while(*from)
  508.     {
  509.         *to = *from;
  510.         to++;
  511.         from++;
  512.     }
  513.     *to = '\0';
  514. }*/
  515.  
  516.  
  517. inline fastcall dword atoi( EDI)
  518. {
  519.     $push ebx
  520.     $push esi
  521.     ESI=EDI;
  522.     while (DSBYTE[ESI]==' ') ESI++;
  523.     if (DSBYTE[ESI]=='-') ESI++;
  524.     EAX=0;
  525.     while (DSBYTE[ESI]>='0') && (DSBYTE[ESI]<='9')
  526.     {
  527.         $xor ebx, ebx
  528.         EBX = DSBYTE[ESI]-'0';
  529.         EAX *= 10;
  530.         EAX += EBX;
  531.         ESI++;
  532.     }
  533.     IF (DSBYTE[EDI]=='-') -EAX;
  534.     $pop esi
  535.     $pop ebx
  536. }
  537.  
  538.  
  539.  
  540. inline fastcall strupr( ESI)
  541. {
  542.     do{
  543.         AL=DSBYTE[ESI];
  544.         IF(AL>='a')IF(AL<='z')DSBYTE[ESI]=AL&0x5f;
  545.         IF (AL>=160) && (AL<=175) DSBYTE[ESI] = AL - 32;    //à-ï
  546.         IF (AL>=224) && (AL<=239) DSBYTE[ESI] = AL - 80;    //à-ï
  547.          ESI++;
  548.     }while(AL!=0);
  549. }
  550.  
  551. inline fastcall strlwr( ESI)
  552. {
  553.     do{
  554.         $LODSB
  555.         IF(AL>='A')&&(AL<='Z'){
  556.             AL+=0x20;
  557.             DSBYTE[ESI-1]=AL;
  558.             CONTINUE;
  559.         }
  560.     }while(AL!=0);
  561. }
  562.  
  563. inline fastcall strttl( EDX)
  564. {
  565.     AL=DSBYTE[EDX];
  566.     IF(AL>='a')&&(AL<='z')DSBYTE[EDX]=AL&0x5f;
  567.     IF (AL>=160) && (AL<=175) DSBYTE[EDX] = AL - 32;    //à-ï
  568.     IF (AL>=224) && (AL<=239) DSBYTE[EDX] = AL - 80;    //à-ï
  569.     do{
  570.         EDX++;
  571.         AL=DSBYTE[EDX];
  572.         IF(AL>='A')&&(AL<='Z'){DSBYTE[EDX]=AL|0x20; CONTINUE;}
  573.         IF(AL>='€')&&(AL<='')DSBYTE[EDX]=AL|0x20; // -¯
  574.         IF (AL>=144) && (AL<=159) DSBYTE[EDX] = AL + 80;    //à-ï
  575.     }while(AL!=0);
  576. }
  577.  
  578. /*
  579. dword itoa( ESI)
  580. {
  581.     unsigned char buffer[11];
  582.     $pusha
  583.     EDI = #buffer;
  584.     ECX = 10;
  585.     if (ESI < 0)
  586.     {
  587.          $mov     al, '-'
  588.          $stosb
  589.          $neg     esi
  590.     }
  591.  
  592.     $mov     eax, esi
  593.     $push    -'0'
  594. F2:
  595.     $xor     edx, edx
  596.     $div     ecx
  597.     $push    edx
  598.     $test    eax, eax
  599.     $jnz     F2
  600. F3:
  601.     $pop     eax
  602.     $add     al, '0'
  603.     $stosb
  604.     $jnz     F3
  605.    
  606.     $mov     al, '\0'
  607.     $stosb
  608.  
  609.     $popa
  610.     return #buffer;
  611. }
  612. */
  613.        
  614. inline dword itoa(signed long number)
  615. {
  616.         unsigned char buf[11];
  617.         dword ret;
  618.         byte cmd;
  619.         long mask,tmp;
  620.         mask = 1000000000;
  621.         cmd = true;
  622.         if(!number){
  623.                 ESBYTE[buf] = '0';
  624.                 ESBYTE[buf+1] = 0;
  625.                 return buf;
  626.         }
  627.         ret = buf;
  628.         if(number<0)
  629.         {
  630.                 $neg number
  631.                 ESBYTE[buf] = '-';
  632.                 $inc buf
  633.         }
  634.         while(mask)
  635.         {
  636.                 tmp = number / mask;
  637.                 tmp = tmp%10;
  638.                
  639.                 if(cmd){
  640.                         if(tmp){
  641.                                 ESBYTE[buf] = tmp + '0';
  642.                                 $inc buf
  643.                                 cmd = false;
  644.                         }
  645.                 }
  646.                 else {
  647.                         ESBYTE[buf] = tmp + '0';
  648.                         $inc buf
  649.                 }
  650.                 mask /= 10;
  651.         }
  652.         ESBYTE[buf] = 0;
  653.         return ret;
  654. }
  655.        
  656. inline fastcall itoa_(signed int EDI, ESI)
  657. {
  658.     $pusha
  659.     EBX = EDI;
  660.     ECX = 10;
  661.     if (ESI > 90073741824)
  662.     {
  663.          $mov     al, '-'
  664.          $stosb
  665.          $neg     esi
  666.     }
  667.  
  668.     $mov     eax, esi
  669.     $push    -'0'
  670. F2:
  671.     $xor     edx, edx
  672.     $div     ecx
  673.     $push    edx
  674.     $test    eax, eax
  675.     $jnz     F2
  676. F3:
  677.     $pop     eax
  678.     $add     al, '0'
  679.     $stosb
  680.     $jnz     F3
  681.    
  682.     $mov     al, '\0'
  683.     $stosb
  684.  
  685.     $popa
  686.     return EBX;
  687. }
  688.  
  689. inline dword strdup(dword text)
  690. {
  691.     dword l = strlen(text);
  692.     dword ret = malloc(l+1);
  693.     strncpy(ret,text,l);
  694.     return ret;
  695. }
  696.  
  697. inline dword strndup(dword str, signed maxlen)
  698. {
  699.         dword copy,len;
  700.  
  701.         len = strnlen(str, maxlen);
  702.         copy = malloc(len + 1);
  703.         if (copy != NULL)
  704.         {
  705.                 memcpy(copy, str, len);
  706.                 DSBYTE[len+copy] = '\0';
  707.         }
  708.         return copy;
  709. }
  710.  
  711. void debugi(dword d_int)
  712. {
  713.     char tmpch[12];
  714.     itoa_(#tmpch, d_int);
  715.     debugln(#tmpch);
  716. }
  717.  
  718.  
  719. //#define strncpy strcpyn
  720. #define strnmov strmovn
  721. #define stricmp strcmpi
  722. #define strcmpn strncmp
  723.  
  724.