[Now closed subject, useless topic in the end]
-
1Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – Chris Feb 15 '20 at 13:11
-
Not directly answering to your question, have you tried Python-C transpiler or compiler? For example, I use Nuitka and it gives a good performance boost in most cases. – Hotte Shen Feb 15 '20 at 13:12
-
You cannot rewrite directly into C, because C has no "counted strings" (and the 4-bye fragment contains a NUL). So you'll need to keep track of the actual size-used of the char[] object. – wildplasser Feb 15 '20 at 13:50
-
`f = f[:c] + f[c+4:]` just erases substring "ROB". To do this in C, simple copy both parts into another string with enough size. Like `oldstr[c]=0; snprintf(newstr, size, "%s%s", oldstr, oldstr+4);` – Eddy_Em Feb 15 '20 at 14:26
-
If I understood right, it should be : `char *var[c] = 0; snprintf(f, sizeof(f), "%s%s", var, var+4);` ? (I wonder if `memcpy()` wouldn't be useful too ?) – Feb 15 '20 at 14:56
1 Answers
Those are "slices", and the + simply concatenates them. This is the simplest case of slicing, [start:end] creates a subsequence of a sequence, starting from position start inclusive, ending at position end, exclusive. If one of them is missing (but the : is there), it is considered to be the beginning/end of the complete sequence. Indices are zero based.
Examples with string:
'Hello World'[1:5]:'ello''Hello World'[:5]:'Hello''Hello World'[1:]:'ello World''Hello World'[:1]+'Hello World'[5:]:'H World'
Your loop steps through your data in 4-byte steps, comparing the 4-byte chunk at the current position with the sequence you have in ROB, and it returns a new sequence without it when it is found (so the f=... part does not actually modify the existing sequence).
In C you could do that with memcmp, then malloc and a pair of memcpy calls, as you correctly noticed that this data is binary (contains null-characters), so string functions would likely break it.
Hopefully I did not messed up the indices, and then it could look like this:
void* my_function(const void* f, int len){
for(int i=0;i<len;i+=4)
if(memcmp(f+i,ROB,4)==0){
void* ret=malloc(len-4);
memcpy(ret,f,i);
memcpy(ret+i,f+i+4,len-i-4);
return ret;
}
return NULL; // or have it die somehow
}
- 11,284
- 3
- 17
- 44
-
@Lisbakc `memcpy(f, ROB, 4);` in particular would copy the first 4 bytes of `ROB` at the beginning of `f`. That may or may not be what you need, I can't tell. – tevemadar Feb 15 '20 at 16:55
-
A side comment: if `len` (length of the original sequence) may be something not divisible by 4, `for(int i=0;i
– tevemadar Feb 15 '20 at 16:57