project_files/hwc/rtl/system.c
changeset 10131 4b4a043111f4
parent 10128 0f6878b5395a
child 10134 81d733899e06
equal deleted inserted replaced
10130:a9d509848390 10131:4b4a043111f4
    64 
    64 
    65     if (index + count > s.len + 1) {
    65     if (index + count > s.len + 1) {
    66         count = s.len + 1 - index;
    66         count = s.len + 1 - index;
    67     }
    67     }
    68 
    68 
    69     memcpy(result.s + 1, s.s + index - 1, count);
    69     memcpy(result.s + 1, s.s + index, count);
    70 
    70 
    71     result.len = count;
    71     result.len = count;
    72 
    72 
    73     return result;
    73     return result;
    74 }
    74 }
    75 
    75 
    76 void fpcrtl_delete__vars(string255 *s, SizeInt index, SizeInt count) {
    76 void __attribute__((overloadable)) fpcrtl_delete__vars(string255 *s, SizeInt index, SizeInt count) {
    77     // number of chars to be move
    77     // number of chars to be move
    78     int num_move;
    78     int num_move;
    79     int new_length;
    79     int new_length;
    80 
    80 
    81     string255 temp = *s;
       
    82 
       
    83     if (index < 1) {
    81     if (index < 1) {
    84         // in fpc, if index < 1, the string won't be modified
    82         // in fpc, if index < 1, the string won't be modified
    85         return;
    83         return;
    86     }
    84     }
    87 
    85 
    88     if(index > s->len){
    86     if(index > s->len){
    89         return;
    87         return;
    90     }
    88     }
    91 
    89 
    92     if (count > s->len - index + 1) {
    90     if (count > s->len - index + 1) {
    93         s->str[index - 1] = 0;
       
    94         s->len = index - 1;
    91         s->len = index - 1;
    95         return;
    92         return;
    96     }
    93     }
    97 
    94 
    98     num_move = s->len - index + 1 - count;
    95     num_move = s->len - index + 1 - count;
    99     new_length = s->len - count;
    96     new_length = s->len - count;
   100 
    97 
   101     memmove(s->str + index - 1, temp.str + index - 1 + count, num_move);
    98     memmove(s->str + index - 1, s->str + index - 1 + count, num_move);
   102     s->str[new_length] = 0;
    99     s->str[new_length] = 0;
       
   100 
       
   101     s->len = new_length;
       
   102 
       
   103 }
       
   104 
       
   105 void __attribute__((overloadable)) fpcrtl_delete__vars(astring *s, SizeInt index, SizeInt count) {
       
   106     // number of chars to be move
       
   107     int num_move;
       
   108     int new_length;
       
   109 
       
   110     if (index < 1) {
       
   111         // in fpc, if index < 1, the string won't be modified
       
   112         return;
       
   113     }
       
   114 
       
   115     if(index > s->len){
       
   116         return;
       
   117     }
       
   118 
       
   119     if (count > s->len - index + 1) {
       
   120         s->len = index - 1;
       
   121         return;
       
   122     }
       
   123 
       
   124     num_move = s->len - index + 1 - count;
       
   125     new_length = s->len - count;
       
   126 
       
   127     memmove(s->s + index, s->s + index + count, num_move);
   103 
   128 
   104     s->len = new_length;
   129     s->len = new_length;
   105 
   130 
   106 }
   131 }
   107 
   132 
   125     return fpcrtl_pos(t, str);
   150     return fpcrtl_pos(t, str);
   126 }
   151 }
   127 
   152 
   128 Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, string255 str) {
   153 Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, string255 str) {
   129 
   154 
   130     char* p;
   155     unsigned char* p;
   131 
   156 
   132     if (str.len == 0) {
   157     if (str.len == 0) {
   133         return 0;
   158         return 0;
   134     }
   159     }
   135 
   160 
   144 
   169 
   145     if (p == NULL) {
   170     if (p == NULL) {
   146         return 0;
   171         return 0;
   147     }
   172     }
   148 
   173 
   149     return strlen(str.str) - strlen(p) + 1;
   174     return p - (unsigned char*)&str.s;
       
   175 }
       
   176 
       
   177 Integer __attribute__((overloadable)) fpcrtl_pos(Char c, astring str) {
       
   178     string255 t;
       
   179     t.len = 1;
       
   180     t.str[0] = c;
       
   181     t.str[1] = 0;
       
   182     return fpcrtl_pos(t, str);
   150 }
   183 }
   151 
   184 
   152 Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, astring str) {
   185 Integer __attribute__((overloadable)) fpcrtl_pos(string255 substr, astring str) {
   153 
   186 
   154     char* p;
   187     unsigned char* p;
   155 
   188 
   156     if (str.len == 0) {
   189     if (str.len == 0) {
   157         return 0;
   190         return 0;
   158     }
   191     }
   159 
   192 
   168 
   201 
   169     if (p == NULL) {
   202     if (p == NULL) {
   170         return 0;
   203         return 0;
   171     }
   204     }
   172 
   205 
   173     return str.len - strlen(p) + 1;
   206     return p - (unsigned char *)&str.s;
   174 }
   207 }
   175 
   208 
   176 Integer fpcrtl_length(string255 s) {
   209 Integer fpcrtl_length(string255 s) {
   177     return s.len;
   210     return s.len;
   178 }
   211 }