Checking, whether string contains only ascii.

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 22 11:46:24 PST 2017


On Wed, Feb 22, 2017 at 11:43:00AM -0800, H. S. Teoh via Digitalmars-d-learn wrote:
[...]
> 	import std.range.primitives;
> 
> 	bool isAsciiOnly(R)(R input)
> 		if (isInputRange!R && is(ElementType!R : dchar))
> 	{
> 		import std.algorithm.iteration : fold;
> 		return input.fold!((a, b) => a && b < 0x80)(true);
> 	}
> 
> 	unittest
> 	{
> 		assert(isAsciiOnly("abcdefg"));
> 		assert(!isAsciiOnly("abcбвг"));
> 	}
[...]

Ah, missing the Exception part:

	void foo(string input)
	{
		if (!input.isAsciiOnly)
			throw new Exception("...");
	}


T

-- 
Why are you blatanly misspelling "blatant"? -- Branden Robinson


More information about the Digitalmars-d-learn mailing list