Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
d source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
dmd 2.078.3
dmd 2.079.0
dmd 2.079.1
dmd 2.080.1
dmd 2.081.2
dmd 2.082.0
dmd 2.089.0
dmd 2.092.0
dmd 2.094.2
dmd nightly
gdc (trunk)
gdc 10.1
gdc 10.2
gdc 10.5
gdc 10.5 (assertions)
gdc 11.1
gdc 11.1 (assertions)
gdc 11.3
gdc 11.3 (assertions)
gdc 11.4
gdc 11.4 (assertions)
gdc 12.1
gdc 12.1 (assertions)
gdc 12.2
gdc 12.2 (assertions)
gdc 12.3
gdc 12.3 (assertions)
gdc 12.4
gdc 12.4 (assertions)
gdc 13.1
gdc 13.1 (assertions)
gdc 13.2
gdc 13.2 (assertions)
gdc 13.3
gdc 13.3 (assertions)
gdc 14.1
gdc 14.1 (assertions)
gdc 14.2
gdc 14.2 (assertions)
gdc 9.2
gdc 9.3
gdc 9.5
gdc arm 12.2.0
gdc arm 12.3.0
gdc arm 12.4.0
gdc arm 13.1.0
gdc arm 13.2.0
gdc arm 13.3.0
gdc arm 14.1.0
gdc arm 14.2.0
gdc arm64 12.2.0
gdc arm64 12.3.0
gdc arm64 12.4.0
gdc arm64 13.1.0
gdc arm64 13.2.0
gdc arm64 13.3.0
gdc arm64 14.1.0
gdc arm64 14.2.0
gdc hppa 14.2.0
gdc loongarch64 14.1.0
gdc loongarch64 14.2.0
gdc mips 12.1.0
gdc mips 12.2.0
gdc mips 12.3.0
gdc mips 12.4.0
gdc mips 13.1.0
gdc mips 13.2.0
gdc mips 13.3.0
gdc mips 14.1.0
gdc mips 14.2.0
gdc mips64 12.1.0
gdc mips64 12.2.0
gdc mips64 12.3.0
gdc mips64 12.4.0
gdc mips64 13.1.0
gdc mips64 13.2.0
gdc mips64 13.3.0
gdc mips64 14.1.0
gdc mips64 14.2.0
gdc mipsel 12.1.0
gdc mipsel 12.2.0
gdc mipsel 12.3.0
gdc mipsel 12.4.0
gdc mipsel 13.1.0
gdc mipsel 13.2.0
gdc mipsel 13.3.0
gdc mipsel 14.1.0
gdc mipsel 14.2.0
gdc powerpc 12.1.0
gdc powerpc 12.2.0
gdc powerpc 12.3.0
gdc powerpc 12.4.0
gdc powerpc 13.1.0
gdc powerpc 13.2.0
gdc powerpc 13.3.0
gdc powerpc 14.1.0
gdc powerpc 14.2.0
gdc powerpc64 12.1.0
gdc powerpc64 12.2.0
gdc powerpc64 12.3.0
gdc powerpc64 12.4.0
gdc powerpc64 13.1.0
gdc powerpc64 13.2.0
gdc powerpc64 13.3.0
gdc powerpc64 14.1.0
gdc powerpc64 14.2.0
gdc powerpc64 trunk
gdc powerpc64le 12.1.0
gdc powerpc64le 12.2.0
gdc powerpc64le 12.3.0
gdc powerpc64le 12.4.0
gdc powerpc64le 13.1.0
gdc powerpc64le 13.2.0
gdc powerpc64le 13.3.0
gdc powerpc64le 14.1.0
gdc powerpc64le 14.2.0
gdc powerpc64le trunk
gdc riscv32 12.2.0
gdc riscv32 12.3.0
gdc riscv32 12.4.0
gdc riscv32 13.1.0
gdc riscv32 13.2.0
gdc riscv32 13.3.0
gdc riscv32 14.1.0
gdc riscv32 14.2.0
gdc riscv32 trunk
gdc riscv64 12.2.0
gdc riscv64 12.3.0
gdc riscv64 12.4.0
gdc riscv64 13.1.0
gdc riscv64 13.2.0
gdc riscv64 13.3.0
gdc riscv64 14.1.0
gdc riscv64 14.2.0
gdc riscv64 trunk
gdc s390x 12.1.0
gdc s390x 12.2.0
gdc s390x 12.3.0
gdc s390x 12.4.0
gdc s390x 13.1.0
gdc s390x 13.2.0
gdc s390x 13.3.0
gdc s390x 14.1.0
gdc s390x 14.2.0
ldc 0.17.2
ldc 1.0.0
ldc 1.1.0
ldc 1.10.0
ldc 1.11.0
ldc 1.12.0
ldc 1.13.0
ldc 1.14.0
ldc 1.15.0
ldc 1.16.0
ldc 1.17.0
ldc 1.18.0
ldc 1.19.0
ldc 1.2.0
ldc 1.20.0
ldc 1.21.0
ldc 1.22.0
ldc 1.23.0
ldc 1.24.0
ldc 1.25.1
ldc 1.26.0
ldc 1.27.1
ldc 1.28.1
ldc 1.29.0
ldc 1.3.0
ldc 1.30.0
ldc 1.31.0
ldc 1.32.1
ldc 1.33.0
ldc 1.34.0
ldc 1.35.0
ldc 1.36.0
ldc 1.37.0
ldc 1.38.0
ldc 1.39.0
ldc 1.4.0
ldc 1.5.0
ldc 1.6.0
ldc 1.7.0
ldc 1.8.0
ldc 1.9.0
ldc beta
ldc latest CI
Options
Source code
import std.string : strip; import std.format : formattedRead; import std.stdio : writeln, File, readln; import std.random : uniform; import std.regex : matchAll; import std.conv : to; void main() { string dictionaryFile = "diceware.txt"; writeln("Welcome to diceware password helper!\n" ~ "v0.1 by Jeremiah Glover, April, 2019."); File wordlist; wordlist.open(dictionaryFile, "r"); string[] dicewords; // read word list file into a slice. int numLines; foreach (line; wordlist.byLine) { ++numLines; int dummy; string word; formattedRead(line, "%d\t%s", dummy, word); dicewords ~= word; } // the lowest value possible is 11111 in base six because dice start at 1, not 0. const int startVal = 1 * 6^^4 + 1 * 6^^3 + 1 * 6^^2 + 1 * 6; // = 1554 // leaving off the one because this will be used as an array index helper. const int maxVal = 6^^5; // 7776 writeln("How many words would you like in your passphrase?"); int num_words; formattedRead(readln.strip, "%d", num_words); writeln("You may begin entering numbers from 1 and 6, pressing enter " ~ "after each.\n Your passphrase will be displayed immediately after " ~ "you enter that last number."); int[] responses; for (int i; i < num_words; ++i) { int word_num = 0; for (int j; j < 5; ++j) { string holder = readln.strip; if(holder.length == 1 && matchAll(holder, `[1-6]{1}`)) { word_num += to!int(holder) * 6 ^^ (4 - j); } else { --j; writeln("Please enter a number between 1 and 6."); } } writeln(word_num - startVal); responses ~= word_num - startVal; } writeln("Your passphrase is "); concatSelectedWords(dicewords, responses).writeln; //readln(); // clear input buffer. while (true) { writeln("Would you like a different password generated based on those"); writeln("numbers? (y/n)"); string quitvar = readln().strip; if (quitvar == "y") { int offset = uniform(0, maxVal); responses[0 .. $] = (responses[0 .. $] + offset) % maxVal; concatSelectedWords(dicewords, responses).writeln; } else if (quitvar == "n") { break; } else { writeln("please respond with y or n."); } } } string concatSelectedWords(string[] words, int[] selections) { string concat; foreach (number; selections) { concat ~= words[number - 1] ~ " "; } return concat; } /** this function is designed to be an intermediary between bases. */ string toDecimal(string number, int base) { int[string] lits = ["0" : 0, "1" : 1, "2" : 2, "3" : 3, "4" : 4, "5" : 5, "6" : 6, "7" : 7, "8" : 8, "9" : 9, "A" : 10, "B" : 11, "C" : 12, "D" : 13, "E" : 14, "F" : 15]; // validate input foreach(digit; number) { if(lits[to!string(digit)] >= base) { return "Error: '" ~ digit ~ "' is greater than or equal to base."; } } // a decimal number is composed of digits that are multiplied by a power of // ten corresponding to how many places from the right it appears in the // number. 1234 = 1 * 10^3 + 2 * 10^2 + 3 * 10^1 + 4 * 10^0; // to convert from any base into decimal, you multiply the decimal // representation of each digit times the decimal representation of the base // to the power of the position from the right taking the rightmost position // to be zero. int result; foreach(i, digit; number) { result += lits[to!string(digit)] * base^^(number.length - (i + 1)); } return to!string(result); } unittest { assert(toDecimal("ABC", 6) == "Error, 'A' is greater than or equal to base."); assert(toDecimal("1234", 10) == "1234"); assert(toDecimal("55555", 6) == "7776"); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree