Subversion Repositories Kolibri OS

Rev

Rev 5573 | Rev 5575 | 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. 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. 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.  
  116. signed int strcmp(dword text1, text2)
  117. {
  118.         char s1,s2;
  119.         dword p1,p2;
  120.         p1 = text1;
  121.         p2 = text2;
  122.         loop()
  123.         {
  124.                 s1 = DSBYTE[text1];
  125.                 s2 = DSBYTE[text2];
  126.                 if(s1==s2)
  127.                 {
  128.                         if(s1==0) return 0;
  129.                 }
  130.                 else {
  131.                         return s1-s2;
  132.                 }
  133.                 $inc text1
  134.                 $inc text2
  135.         }
  136.         return 0;
  137. }
  138.  
  139.  
  140. inline fastcall void strcpy( EDI, ESI)
  141. {
  142.     $cld
  143. L2:
  144.     $lodsb
  145.     $stosb
  146.     $test al,al
  147.     $jnz L2
  148. }
  149.  
  150. void strncpy(dword text1, text2, signed len)
  151.         signed o1,o2;
  152. {
  153.         o1 = len/4;
  154.         o2 = len-4*o1;
  155.         while(o1){
  156.                 ESDWORD[text1] = ESDWORD[text2];
  157.                 text1 += 4;
  158.                 text2 += 4;
  159.                 $dec o1
  160.         }
  161.         while(o2){
  162.                 ESBYTE[text1] = ESBYTE[text2];
  163.                 $inc text1
  164.                 $inc text2
  165.                 $dec o2
  166.         }
  167. }
  168.  
  169. inline fastcall int strlcpy(dword ESI, EDI, EBX)
  170. {
  171.     if (EBX<0) return -1;
  172.     EDX=0;
  173.     do {
  174.         DSBYTE[ESI]=DSBYTE[EDI];
  175.         ESI++;
  176.         EDI++;
  177.         EDX++;
  178.         if (EDX==EBX) { DSBYTE[ESI]='\0'; return -1;}
  179.     } while(DSBYTE[EDI-1]!='\0');
  180.     return 0;
  181. }
  182.  
  183. /*
  184. inline fastcall void strtrim( ESI)
  185. {
  186.     EDI = ESI;
  187.     do{
  188.         AL=DSBYTE[EDI];
  189.         if (AL != '\32') && (AL != '\13') && (AL != '\10')
  190.         {
  191.             DSBYTE[ESI]=AL;
  192.             $inc ESI
  193.         }
  194.          $inc EDI
  195.     }while(AL!=0);
  196.     DSBYTE[ESI] = '\0';
  197. }
  198. */
  199.  
  200. byte __isWhite(int s){ if (s==13)||(s==32)||(s==10)||(s==9) return true; return false; }
  201. void strltrim(dword text){
  202.         int s;
  203.         dword back_text;
  204.         back_text = text;
  205.         s = ESBYTE[text];
  206.         while(__isWhite(s))
  207.         {
  208.                 $inc text
  209.                 s = ESBYTE[text];
  210.         }
  211.         loop()
  212.         {
  213.                 ESBYTE[back_text] = s;
  214.                 $inc back_text
  215.                 if(!s) break;
  216.                 $inc text
  217.                 s = ESBYTE[text];
  218.         };
  219. }
  220.  
  221. void strrtrim(dword text)
  222. {
  223.         int s;
  224.         dword p;
  225.         do {
  226.                 s = ESBYTE[text];
  227.                 if(__isWhite(s))
  228.                 {
  229.                         p = text;
  230.                         while(__isWhite(s))
  231.                         {
  232.                                 $inc text;
  233.                                 s = ESBYTE[text];
  234.                         }
  235.                 }
  236.                 else $inc text
  237.         } while(s);
  238.         $dec text
  239.         s = ESBYTE[text];
  240.         if(__isWhite(s)) ESBYTE[p] = 0;
  241. }
  242.  
  243. void strtrim(dword text){
  244.         int s;
  245.         dword p,back_text;
  246.         back_text = text;
  247.         s = ESBYTE[text];
  248.         while(__isWhite(s))
  249.         {
  250.                 $inc text
  251.                 s = ESBYTE[text];
  252.         }
  253.         do {
  254.                 s = ESBYTE[text];
  255.                 if(__isWhite(s))
  256.                 {
  257.                         p = back_text;
  258.                         while(__isWhite(s))
  259.                         {
  260.                                 ESBYTE[back_text] = s;
  261.                                 $inc back_text
  262.                                 $inc text;
  263.                                 s = ESBYTE[text];
  264.                         }
  265.                 }
  266.                 else {
  267.                         ESBYTE[back_text] = s;
  268.                         $inc back_text
  269.                         $inc text
  270.                 }
  271.         } while(s);
  272.         $dec text
  273.         s = ESBYTE[text];
  274.         if(__isWhite(s)) ESBYTE[p] = 0;
  275. }
  276.  
  277. inline fastcall void strcat( EDI, ESI)
  278. {
  279.   asm {
  280.     mov ebx, edi
  281.     xor ecx, ecx
  282.     xor eax, eax
  283.     dec ecx
  284.     repne scasb
  285.     dec edi
  286.     mov edx, edi
  287.     mov edi, esi
  288.     xor ecx, ecx
  289.     xor eax, eax
  290.     dec ecx
  291.     repne scasb
  292.     xor ecx, 0ffffffffh
  293.     mov edi, edx
  294.     mov edx, ecx
  295.     mov eax, edi
  296.     shr ecx, 2
  297.     rep movsd
  298.     mov ecx, edx
  299.     and ecx, 3
  300.     rep movsb
  301.     mov eax, ebx
  302.     }
  303. }
  304.  
  305. void strncat(dword text1, text2, signed len)
  306.         signed o1,o2;
  307.         char s;
  308. {
  309.         s = ESBYTE[text1];
  310.         while(s){
  311.                 $inc text1
  312.                 s = ESBYTE[text1];
  313.         }
  314.         o1 = len/4;
  315.         o2 = len-4*o1;
  316.         while(o1){
  317.                 ESDWORD[text1] = ESDWORD[text2];
  318.                 text1 += 4;
  319.                 text2 += 4;
  320.                 $dec o1
  321.         }
  322.         while(o2){
  323.                 ESBYTE[text1] = ESBYTE[text2];
  324.                 $inc text1
  325.                 $inc text2
  326.                 $dec o2
  327.         }
  328. }
  329.  
  330. inline fastcall void chrcat(ESI, BL)
  331. {
  332.     EDI = strlen(ESI);
  333.     ESBYTE[ESI+EDI] = BL;
  334.     ESBYTE[ESI+EDI+1] = 0;
  335. }
  336.  
  337.  
  338. inline fastcall signed int strchr( ESI,BL)
  339. {
  340.     int jj=0;
  341.     do{
  342.         jj++;
  343.         $lodsb
  344.         IF(AL==BL) return jj;
  345.     } while(AL!=0);
  346.     return 0;
  347. }
  348.  
  349.  
  350. inline fastcall signed int strrchr( ESI,BL)
  351. {
  352.     int jj=0, last=0;
  353.     do{
  354.         jj++;
  355.         $lodsb
  356.         IF(AL==BL) last=jj;
  357.     } while(AL!=0);
  358.     return last;
  359. }
  360.  
  361.  
  362. int chrnum(dword searchin, char symbol)
  363. {
  364.     int num = 0;
  365.     while(DSBYTE[searchin])
  366.     {
  367.         if (DSBYTE[searchin] == symbol)    num++;
  368.         searchin++;
  369.     }
  370.     return num;
  371. }
  372.  
  373.  
  374. inline fastcall signed int strstr( EBX, EDX)
  375. {
  376.   asm {
  377.     MOV EDI, EDX
  378.     XOR ECX, ECX
  379.     XOR EAX, EAX
  380.     DEC ECX
  381.     REPNE SCASB
  382.     NOT ECX
  383.     DEC ECX
  384.     JE LS2
  385.     MOV ESI, ECX
  386.     XOR ECX, ECX
  387.     MOV EDI, EBX
  388.     DEC ECX
  389.     REPNE SCASB
  390.     NOT ECX
  391.     SUB ECX, ESI
  392.     JBE LS2
  393.     MOV EDI, EBX
  394.     LEA EBX, DSDWORD[ ESI-1]
  395. LS1: MOV ESI, EDX
  396.     LODSB
  397.     REPNE SCASB
  398.     JNE LS2
  399.     MOV EAX, ECX
  400.     PUSH EDI
  401.     MOV ECX, EBX
  402.     REPE CMPSB
  403.     POP EDI
  404.     MOV ECX, EAX
  405.     JNE LS1
  406.     LEA EAX, DSDWORD[ EDI-1]
  407.     JMP SHORT LS3
  408. LS2: XOR EAX, EAX
  409. LS3:
  410.   }
  411. }
  412.  
  413. dword strcmpi(dword cmp1, cmp2)
  414. {
  415.     char si, ue;
  416.  
  417.     loop()
  418.     {
  419.         si = DSBYTE[cmp1];
  420.         ue = DSBYTE[cmp2];
  421.         if (si>='A') && (si<='Z') si +=32;
  422.         if (ue>='A') && (ue<='Z') ue +=32;
  423.         if (si != ue) return -1;
  424.         cmp1++;
  425.         cmp2++;
  426.         if ((DSBYTE[cmp1]=='\0') && (DSBYTE[cmp2]=='\0')) return 0;
  427.         if (DSBYTE[cmp1]=='\0') return -1;
  428.         if (DSBYTE[cmp2]=='\0') return 1;
  429.     }
  430. }
  431.  
  432. dword strstri(dword searchin, usestr_s)
  433. {
  434.     dword usestr_e = usestr_s;
  435.     char si, ue;
  436.  
  437.     while(DSBYTE[searchin])
  438.     {
  439.         si = DSBYTE[searchin];
  440.         ue = DSBYTE[usestr_e];
  441.         if (si>='A') && (si<='Z') si +=32;
  442.         if (ue>='A') && (ue<='Z') ue +=32;
  443.         if (si == ue) usestr_e++; else usestr_e = usestr_s;
  444.         searchin++;
  445.         if (DSBYTE[usestr_e]=='\0') return searchin;
  446.     }
  447.     return 0;
  448. }
  449.  
  450.  
  451. unsigned int strcpyb(dword search_in, copyin, startstr, endstr)
  452. {
  453.     dword startp, endp;
  454.     dword copyin_start_off = copyin;
  455.     if (startstr==0) startp = search_in; else startp = strstr(search_in, startstr) + strlen(startstr);
  456.     endp = strstri(startp, endstr);
  457.     if (endp==0) endp = startp+strlen(search_in);
  458.     //if (startp==endp) return 0;
  459.     do
  460.     {
  461.         DSBYTE[copyin] = DSBYTE[startp];
  462.         copyin++;
  463.         startp++;
  464.     }
  465.     while (startp<endp);
  466.     DSBYTE[copyin] = '\0';
  467.     return copyin_start_off;
  468. }
  469.  
  470.  
  471. /*void strcat(char *to, char *from)
  472. {
  473.     while(*to) to++;
  474.     while(*from)
  475.     {
  476.         *to = *from;
  477.         to++;
  478.         from++;
  479.     }
  480.     *to = '\0';
  481. }*/
  482.  
  483.  
  484. inline fastcall dword atoi( EDI)
  485. {
  486.     $push ebx
  487.     $push esi
  488.     ESI=EDI;
  489.     while (DSBYTE[ESI]==' ') ESI++;
  490.     if (DSBYTE[ESI]=='-') ESI++;
  491.     EAX=0;
  492.     while (DSBYTE[ESI]>='0') && (DSBYTE[ESI]<='9')
  493.     {
  494.         $xor ebx, ebx
  495.         EBX = DSBYTE[ESI]-'0';
  496.         EAX *= 10;
  497.         EAX += EBX;
  498.         ESI++;
  499.     }
  500.     IF (DSBYTE[EDI]=='-') -EAX;
  501.     $pop esi
  502.     $pop ebx
  503. }
  504.  
  505.  
  506.  
  507. inline fastcall strupr( ESI)
  508. {
  509.     do{
  510.         AL=DSBYTE[ESI];
  511.         IF(AL>='a')IF(AL<='z')DSBYTE[ESI]=AL&0x5f;
  512.         IF (AL>=160) && (AL<=175) DSBYTE[ESI] = AL - 32;    //à-ï
  513.         IF (AL>=224) && (AL<=239) DSBYTE[ESI] = AL - 80;    //à-ï
  514.          ESI++;
  515.     }while(AL!=0);
  516. }
  517.  
  518. inline fastcall strlwr( ESI)
  519. {
  520.     do{
  521.         $LODSB
  522.         IF(AL>='A')&&(AL<='Z'){
  523.             AL+=0x20;
  524.             DSBYTE[ESI-1]=AL;
  525.             CONTINUE;
  526.         }
  527.     }while(AL!=0);
  528. }
  529.  
  530. inline fastcall strttl( EDX)
  531. {
  532.     AL=DSBYTE[EDX];
  533.     IF(AL>='a')&&(AL<='z')DSBYTE[EDX]=AL&0x5f;
  534.     IF (AL>=160) && (AL<=175) DSBYTE[EDX] = AL - 32;    //à-ï
  535.     IF (AL>=224) && (AL<=239) DSBYTE[EDX] = AL - 80;    //à-ï
  536.     do{
  537.         EDX++;
  538.         AL=DSBYTE[EDX];
  539.         IF(AL>='A')&&(AL<='Z'){DSBYTE[EDX]=AL|0x20; CONTINUE;}
  540.         IF(AL>='€')&&(AL<='')DSBYTE[EDX]=AL|0x20; // -¯
  541.         IF (AL>=144) && (AL<=159) DSBYTE[EDX] = AL + 80;    //à-ï
  542.     }while(AL!=0);
  543. }
  544.  
  545. /*
  546. dword itoa( ESI)
  547. {
  548.     unsigned char buffer[11];
  549.     $pusha
  550.     EDI = #buffer;
  551.     ECX = 10;
  552.     if (ESI < 0)
  553.     {
  554.          $mov     al, '-'
  555.          $stosb
  556.          $neg     esi
  557.     }
  558.  
  559.     $mov     eax, esi
  560.     $push    -'0'
  561. F2:
  562.     $xor     edx, edx
  563.     $div     ecx
  564.     $push    edx
  565.     $test    eax, eax
  566.     $jnz     F2
  567. F3:
  568.     $pop     eax
  569.     $add     al, '0'
  570.     $stosb
  571.     $jnz     F3
  572.    
  573.     $mov     al, '\0'
  574.     $stosb
  575.  
  576.     $popa
  577.     return #buffer;
  578. }
  579. */
  580.        
  581. dword itoa(signed long number)
  582. {
  583.         unsigned char buf[11];
  584.         dword ret;
  585.         byte cmd;
  586.         long mask,tmp;
  587.         mask = 1000000000;
  588.         cmd = true;
  589.         if(!number){
  590.                 ESBYTE[buf] = '0';
  591.                 ESBYTE[buf+1] = 0;
  592.                 return buf;
  593.         }
  594.         ret = buf;
  595.         if(number<0)
  596.         {
  597.                 $neg number
  598.                 ESBYTE[buf] = '-';
  599.                 $inc buf
  600.         }
  601.         while(mask)
  602.         {
  603.                 tmp = number / mask;
  604.                 tmp = tmp%10;
  605.                
  606.                 if(cmd){
  607.                         if(tmp){
  608.                                 ESBYTE[buf] = tmp + '0';
  609.                                 $inc buf
  610.                                 cmd = false;
  611.                         }
  612.                 }
  613.                 else {
  614.                         ESBYTE[buf] = tmp + '0';
  615.                         $inc buf
  616.                 }
  617.                 mask /= 10;
  618.         }
  619.         ESBYTE[buf] = 0;
  620.         return ret;
  621. }
  622.        
  623. inline fastcall itoa_(signed int EDI, ESI)
  624. {
  625.     $pusha
  626.     EBX = EDI;
  627.     ECX = 10;
  628.     if (ESI > 90073741824)
  629.     {
  630.          $mov     al, '-'
  631.          $stosb
  632.          $neg     esi
  633.     }
  634.  
  635.     $mov     eax, esi
  636.     $push    -'0'
  637. F2:
  638.     $xor     edx, edx
  639.     $div     ecx
  640.     $push    edx
  641.     $test    eax, eax
  642.     $jnz     F2
  643. F3:
  644.     $pop     eax
  645.     $add     al, '0'
  646.     $stosb
  647.     $jnz     F3
  648.    
  649.     $mov     al, '\0'
  650.     $stosb
  651.  
  652.     $popa
  653.     return EBX;
  654. }
  655.  
  656. dword strdup(dword text)
  657. {
  658.     dword l = strlen(text);
  659.     dword ret = malloc(l+1);
  660.     strncpy(ret,text,l);
  661.     return ret;
  662. }
  663.  
  664. void debugi(dword d_int)
  665. {
  666.     char tmpch[12];
  667.     itoa_(#tmpch, d_int);
  668.     debugln(#tmpch);
  669. }
  670.  
  671.  
  672. //#define strncpy strcpyn
  673. #define strnmov strmovn
  674. #define stricmp strcmpi
  675. #define strcmpn strncmp
  676.  
  677.