# dmd.utils

This module defines some utility functions for DMD.

#### Authors

Walter Bright

#### License

Boost License 1.0

Source: utils.d

Documentation: https://dlang.org/phobos/dmd_utils.html

Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/utils.d

- `toWinPath`

  #### Declaration

  `const(char)* `toWinPath`(const(char)* `src`);`

  Normalize path by turning forward slashes into backslashes

  #### Parameters

  `const(char)* `src`` | Source path, using unix-style ('/') path separators

  #### Return Value

  A newly-allocated string with '/' turned into backslashes

- `toPosixPath`

  #### Declaration

  `nothrow @trusted const(char)* `toPosixPath`(const(char)* `src`);
  pure nothrow @safe const(char)[] `toPosixPath`(const(char)[] `src`);`

  Normalize path by turning backslashes into forward slashes

  #### Parameters

  `const(char)* `src`` | Source path, using window-style ('') or mixed path separators

  #### Return Value

  A newly-allocated string with '' turned into '/'

  #### Examples

  1. `const(char)[] nullStr; const(char)* nullStrP; assert(toPosixPath(nullStrP) == null); assert(toPosixPath(nullStr) == null); @trusted nothrow void assertion(const(char)[] s1, const(char)[] s2) { assert(toPosixPath(s1) == s2); assert(strcmp(toPosixPath(s1.ptr), s2.ptr) == 0); } const posixAbsPath = `/some/path`; const posixRelPath = `some/path`; const windowsAbsPath = `C:\some\path`; const windowsAbsMixedPath = `C:\some/path`; const windowsAbsPosixPath = `C:/some/path`; const windowsRelPath = `some\path`; assertion(posixAbsPath, posixAbsPath); assertion(posixRelPath, posixRelPath); assertion(windowsAbsPath, windowsAbsPosixPath); assertion(windowsAbsMixedPath, windowsAbsPosixPath); assertion(windowsAbsPosixPath, windowsAbsPosixPath); assertion(windowsRelPath, posixRelPath);`
- `readFile`

  #### Declaration

  `FileBuffer `readFile`(Loc `loc`, const(char)* `filename`);
  FileBuffer `readFile`(Loc `loc`, const(char)[] `filename`);`

  Reads a file, terminate the program on error

  #### Parameters

  `Loc `loc`` | The line number information from where the call originates
  `const(char)* `filename`` | Path to file

- `writeFile`

  #### Declaration

  `void `writeFile`(Loc `loc`, const(char)[] `filename`, const void[] `data`);`

  Writes a file, terminate the program on error

  #### Parameters

  `Loc `loc`` | The line number information from where the call originates
  `const(char)[] `filename`` | Path to file
  `void[] `data`` | Full content of the file to be written

- `ensurePathToNameExists`

  #### Declaration

  `void `ensurePathToNameExists`(Loc `loc`, const(char)[] `name`);`

  Ensure the root path (the path minus the `name`) of the provided path exists, and terminate the process if it doesn't.

  #### Parameters

  `Loc `loc`` | The line number information from where the call originates
  `const(char)[] `name`` | a path to check (the `name` is stripped)

- `escapePath`

  #### Declaration

  `void `escapePath`(OutBuffer* `buf`, const(char)* `fname`);`

  Takes a path, and escapes '(', ')' and backslashes

  #### Parameters

  `OutBuffer* `buf`` | Buffer to write the escaped path to
  `const(char)* `fname`` | Path to escape

- `parseDigits`

  #### Declaration

  `pure nothrow @nogc @safe bool `parseDigits`(T)(ref T `val`, const(char)[] `p`, const T `max` = T.`max`);`

  Convert string to integer.

  #### Parameters

  `T` | Type of integer to parse
  `T `val`` | Variable to store the result in
  `const(char)[] `p`` | slice to start of string digits
  `T `max`` | `max` allowable value (inclusive), defaults to `T.`max``

  #### Return Value

  `false` on error, `true` on success

  #### Examples

  1. `byte b; ubyte ub; short s; ushort us; int i; uint ui; long l; ulong ul; assert(b.parseDigits("42") && b == 42); assert(ub.parseDigits("42") && ub == 42); assert(s.parseDigits("420") && s == 420); assert(us.parseDigits("42000") && us == 42_000); assert(i.parseDigits("420000") && i == 420_000); assert(ui.parseDigits("420000") && ui == 420_000); assert(l.parseDigits("42000000000") && l == 42_000_000_000); assert(ul.parseDigits("82000000000") && ul == 82_000_000_000); assert(!b.parseDigits(ubyte.max.stringof)); assert(!b.parseDigits("WYSIWYG")); assert(!b.parseDigits("-42")); assert(!b.parseDigits("200")); assert(ub.parseDigits("200") && ub == 200); assert(i.parseDigits(int.max.stringof) && i == int.max); assert(i.parseDigits("420", 500) && i == 420); assert(!i.parseDigits("420", 400));`