ustrfmt.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (C) 2001-2006, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. */
  9. #include "cstring.h"
  10. #include "ustrfmt.h"
  11. /***
  12. * Fills in a char16_t* string with the radix-based representation of a
  13. * uint32_t number padded with zeroes to minwidth. The result
  14. * will be null terminated if there is room.
  15. *
  16. * @param buffer char16_t buffer to receive result
  17. * @param capacity capacity of buffer
  18. * @param i the unsigned number to be formatted
  19. * @param radix the radix from 2..36
  20. * @param minwidth the minimum width. If the result is narrower than
  21. * this, '0's will be added on the left. Must be <=
  22. * capacity.
  23. * @return the length of the result, not including any terminating
  24. * null
  25. */
  26. U_CAPI int32_t U_EXPORT2
  27. uprv_itou (char16_t * buffer, int32_t capacity,
  28. uint32_t i, uint32_t radix, int32_t minwidth)
  29. {
  30. int32_t length = 0;
  31. int digit;
  32. int32_t j;
  33. char16_t temp;
  34. do{
  35. digit = (int)(i % radix);
  36. buffer[length++]=(char16_t)(digit<=9?(0x0030+digit):(0x0030+digit+7));
  37. i=i/radix;
  38. } while(i && length<capacity);
  39. while (length < minwidth){
  40. buffer[length++] = (char16_t) 0x0030;/*zero padding */
  41. }
  42. /* null terminate the buffer */
  43. if(length<capacity){
  44. buffer[length] = (char16_t) 0x0000;
  45. }
  46. /* Reverses the string */
  47. for (j = 0; j < (length / 2); j++){
  48. temp = buffer[(length-1) - j];
  49. buffer[(length-1) - j] = buffer[j];
  50. buffer[j] = temp;
  51. }
  52. return length;
  53. }