[linux-cifs-client] Re: [PATCH] cifs: add helper to simplify unicode to NLS conversion and use it (try #2)

Peter Hudec PeterHudec at web.de
Mon Apr 13 12:15:59 GMT 2009


Jeff Layton schrieb:
>>  /*
>> + * UniStrnlenBytes: Return the length of a NLS string in bytes. Also, populates
>> + * 'nchars' with the length of string in 16 bit Unicode chars.
>> + */
>> +static inline size_t
>> +UniStrnlenBytes(const wchar_t *str, int maxlen, int *nchars,
>> +		const struct nls_table *codepage)
>> +{
>> +	int nc;
>> +	size_t i = 0, nbytes = 0;
>> +	wchar_t uni = *str;
>> +	char buf[NLS_MAX_CHARSET_SIZE]; /* enough for one char at a time */
>> +
>> +	while (*str++ && maxlen) {
>> +		nc = codepage->uni2char(uni, buf, NLS_MAX_CHARSET_SIZE);
>> +		if (nc > 0)
>> +			nbytes += nc;
>> +		else
>> +			nbytes += 1; /* for '?' */
>> +		i++;
>> +		if (i >= maxlen)
>> +			break;
>> +	}
>> +	*nchars = i;
>>     
>
> Peter has a good point that we can get nchars by dividing the input
> string size by 2. It might mean a few less operations but I doubt it
> will be measurable.
Well, I think I got confused myself, because nbytes is not the input 
size, but the output size.
We actually do need the number of iterations of the while statement, but 
in my eyes we can do it directly.
(*nchars)++ in the loop should do it.


Peter


More information about the linux-cifs-client mailing list