lundi 30 mars 2015

Concern of a string conversion function

I made a conversion function from a proprietary text format to a simple text string with escaped unicode codepoints (in the form `\uXXXX' where XXXX is the unicode codepoint in hex format).



int wchar_to_utf16(wchar_t* strIn, char* strOut, int max_buf_len);


In this function I pass the pointer to the string to be converted, the pointer to the destination buffer (in which the converted string will be written) and the length of such buffer.


Inside the function there are buffer bound checks all over the place, if the space is not enough the function returns 1 otherwise 0.


My question is: is a concern of my function to know the buffer length and perform the checks or it's better to remove the length parameter and do the check on the caller? Problem: The minimum buffer length can only be determined looking at the input string and knowing the encoding (which should not be a concern of the caller)


Aucun commentaire:

Enregistrer un commentaire