/src/rt/util/string.d

http://github.com/AlexeyProkhin/druntime · D · 59 lines · 36 code · 11 blank · 12 comment · 4 complexity · 7a688c2b86c5813046493c1e36d7e70d MD5 · raw file

  1. /**
  2. * String manipulation and comparison utilities.
  3. *
  4. * Copyright: Copyright Sean Kelly 2005 - 2009.
  5. * License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
  6. * Authors: Sean Kelly
  7. */
  8. /* Copyright Sean Kelly 2005 - 2009.
  9. * Distributed under the Boost Software License, Version 1.0.
  10. * (See accompanying file LICENSE or copy at
  11. * http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. module rt.util.string;
  14. private import core.stdc.string;
  15. @trusted:
  16. pure:
  17. nothrow:
  18. char[] uintToString( char[] buf, uint val )
  19. {
  20. assert( buf.length > 9 );
  21. auto p = buf.ptr + buf.length;
  22. do
  23. {
  24. *--p = cast(char)(val % 10 + '0');
  25. } while( val /= 10 );
  26. return buf[p - buf.ptr .. $];
  27. }
  28. char[] uintToString( char[] buf, ulong val )
  29. {
  30. assert( buf.length >= 20 );
  31. auto p = buf.ptr + buf.length;
  32. do
  33. {
  34. *--p = cast(char)(val % 10 + '0');
  35. } while( val /= 10 );
  36. return buf[p - buf.ptr .. $];
  37. }
  38. int dstrcmp( in char[] s1, in char[] s2 )
  39. {
  40. int ret = 0;
  41. auto len = s1.length;
  42. if( s2.length < len )
  43. len = s2.length;
  44. if( 0 != (ret = memcmp( s1.ptr, s2.ptr, len )) )
  45. return ret;
  46. return s1.length > s2.length ? 1 :
  47. s1.length == s2.length ? 0 : -1;
  48. }