Re: [Libclc-developers] Re: contribution(?): string function ultostr()
Status: Planning
Brought to you by:
augestad
|
From: Michael B.A. <mb...@io...> - 2003-03-18 07:55:35
|
On Mon, 17 Mar 2003 17:56:05 +0100 (MET) Jan Engelhardt <je...@li...> wrote: > >> /*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D > >> ultostr > >> by Jan "Hirogen2" Engelhardt <hirogen2 at gmx de>, 2003 > >> -- distributed under the Frontier Artistic License and GNU General P= ublic > >> -- License. See doc/FAL1.txt and doc/GPL2.txt for details. > > > >The license must be changed to BSD. > What's the thing about BSD? http://www.opensource.org/licenses/bsd-license.php > >> SYNOPSIS > >> unsigned char *ultostr(unsigned long num, unsigned long base, > >> unsigned char *ptr, size_t size); > >unsigned long base is pretty long for the range 2..36 :-) strtoul uses > >int for base. > woho... heh right, that probably should have been unsigned char. If you were very space conscientious that would indeed be the correct datatype. However I think I would use strtoul as a guide here and just use int. > >Unsigned char* are very uncommon in C libraries, and its buddy function > >strtoul uses char*. > And? I like it. It's probably because I cannot feel comfortable when they > assign the '=F6' a value of -10. Functions that take char * for historical reasons will take care to properly convert each character as necessary taking into consideration what the locale encoding is. Using unsigned char * is appropriate for encoding and decoding types of function however in this context considering we're only expecting ASCII character coupled with the fact that again strtoul should probably be used as a model I suggest char *. So just: char * clc_ultostr(unsigned long num, int base, char *ptr, size_t size) { ... > The major aspects of my style are >=20 > >unsigned char *ultostr( > > unsigned long num, > > unsigned long base, > > unsigned char *ptr, > > size_t size) > - one row, or indent-by-1 if longer than 79 chars Sounds good here too. >=20 > >{ > - as well as keeping any { on the line The prevailing technique is to place the bracket on a new line I believe. Some editors actually key on these (vi for one). > > unsigned char *sym =3D "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", > > > > clc_assert_not_null(ultostr, ptr); > > clc_assert_arg(ultostr, base >=3D 2 && base <=3D 36); > > > > *startp =3D ptr + size - 2; What would happen if size were 2, 1, or even 0? Those are legitimate values. > > if(base < 2 || base > 36) { > > errno =3D ERANGE; > > return NULL; > > } > > > > while(--size > 0) { If size were originally 0 size would rollover to a vary high number. > > *ptr =3D sym[num % base]; > > num /=3D base; > > --ptr; > > } What about negative values? Mike --=20 A program should be written to model the concepts of the task it performs rather than the physical world or a process because this maximizes the potential for it to be applied to tasks that are conceptually similar and, more important, to tasks that have not yet been conceived.=20 |