Thread overview
countUntil with negated pre-defined predicate?
May 02, 2020
Robert M. Münch
May 02, 2020
Harry Gillanders
May 02, 2020
Harry Gillanders
May 03, 2020
Robert M. Münch
May 03, 2020
Harry Gillanders
May 04, 2020
Ali Çehreli
May 04, 2020
Robert M. Münch
May 02, 2020
This works:

	countUntil!(std.uni.isWhite)("hello world"))

How can I switch this to (not working);

	countUntil!(!std.uni.isWhite)("hello world"))

without having to write my own predicate?

Or is there an even better way to search for all "drawable unicode characters"?

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster

May 02, 2020
On 5/2/20 2:23 PM, Robert M. Münch wrote:
> This works:
> 
>      countUntil!(std.uni.isWhite)("hello world"))
> 
> How can I switch this to (not working);
> 
>      countUntil!(!std.uni.isWhite)("hello world"))
> 
> without having to write my own predicate?
> 
> Or is there an even better way to search for all "drawable unicode characters"?
> 

Write your own predicate, it's not much different:

countUntil!(c => !std.uni.isWhite(c))("hello world")

-Steve
May 02, 2020
On Saturday, 2 May 2020 at 18:23:30 UTC, Robert M. Münch wrote:
> This works:
>
> 	countUntil!(std.uni.isWhite)("hello world"))
>
> How can I switch this to (not working);
>
> 	countUntil!(!std.uni.isWhite)("hello world"))
>
> without having to write my own predicate?
>
> Or is there an even better way to search for all "drawable unicode characters"?

std.functional.not can do this: https://dlang.org/phobos/std_functional.html#not
May 02, 2020
On Saturday, 2 May 2020 at 18:23:30 UTC, Robert M. Münch wrote:
> Or is there an even better way to search for all "drawable unicode characters"?

This depends on what you classify as drawable, and what you consider to be a character (the joys of Unicode), and why you want to search for them anyway.

One way (I haven't verified this) could be to check if any of the code-points within a grapheme are graphical[1], and not white-space (and are not any other code-point you consider non-drawable).

Which could look like so:

	import std.algorithm;
	import std.range;
	import std.uni;

	size_t drawableCharacterCount (CodePoints) (auto ref CodePoints codePoints)
	if (isInputRange!CodePoints && is(ElementType!CodePoints : dchar))
	{
		bool isDrawableCodePoint (dchar c)
		{
			return c.isGraphical() && !c.isWhite();
		}

		return codePoints.byGrapheme().count!(
			g => g[].any!isDrawableCodePoint
		);
	}

[1]: https://www.unicode.org/versions/Unicode13.0.0/ch02.pdf#G286941

---
The source-code in this reply is available for use under the terms of Creative Commons CC0 1.0 Universal.


May 03, 2020
On 2020-05-02 22:33:59 +0000, Harry Gillanders said:

> This depends on what you classify as drawable, and what you consider to be a character (the joys of Unicode),

Absolutely... however, trying getting close to what works in most cases.

> and why you want to search for them anyway.

I'm doing some cursor-movement in a text-field. So, need to find out where the cursor should be positioned.

> One way (I haven't verified this) could be to check if any of the code-points within a grapheme are graphical[1], and not white-space (and are not any other code-point you consider non-drawable).

Yes, that makes sense. I wasn't aware about graphical category... so would have used the !isWhite approach only. Thanks.

> Which could look like so:
> 
> 	import std.algorithm;
> 	import std.range;
> 	import std.uni;
> 
> 	size_t drawableCharacterCount (CodePoints) (auto ref CodePoints codePoints)

What does this line do?

> 	if (isInputRange!CodePoints && is(ElementType!CodePoints : dchar))
> 	{
> 		bool isDrawableCodePoint (dchar c)
> 		{
> 			return c.isGraphical() && !c.isWhite();
> 		}
> 
> 		return codePoints.byGrapheme().count!(
> 			g => g[].any!isDrawableCodePoint
> 		);
> 	}

I want to identify spans of drawable and isWhite in a grapheme array. So, I think any! just gives the total count of the whole thing. But anyway, thanks for the input, helps to better understand the whole thing.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster

May 03, 2020
On Sunday, 3 May 2020 at 12:19:30 UTC, Robert M. Münch wrote:
>
> I'm doing some cursor-movement in a text-field. So, need to
> find out where the cursor should be positioned.
>

The Unicode Consortium has some documentation related to segmenting text that
you may find useful (notably, section 3): https://unicode.org/reports/tr29

>> 	size_t drawableCharacterCount (CodePoints) (auto ref CodePoints codePoints)
>
> What does this line do?

I'm unsure as to which part is unclear, but an `auto ref` parameter[1] in a
function template is essentially a parameter that receives the
argument by reference if the templated type is a value-type,
whereas if the templated type is a reference-type, it receives the argument by value.

An explanation as code:

	struct SomeValueType
	{}

	class SomeReferenceType
	{}

	void theFunction (Type) (auto ref Type thing)
	{}


	/+ theFunction!SomeValueType expands to: +/
	void theFunction (ref SomeValueType thing)
	{}

	/+ theFunction!SomeReferenceType expands to: +/
	void theFunction (SomeReferenceType thing)
	{}

One advantage of auto ref parameters is that they accept literals as arguments,
unlike a regular `ref` parameter.

[1]: https://dlang.org/spec/template.html#auto-ref-parameters


May 04, 2020
On 5/3/20 2:59 PM, Harry Gillanders wrote:> On Sunday, 3 May 2020 at 12:19:30 UTC, Robert M. Münch wrote:

> an `auto ref` parameter[1] in a
> function template is essentially a parameter that receives the
> argument by reference if the templated type is a value-type,

Please replace "a value-type" above with "an lvalue".

> whereas if the templated type is a reference-type, it receives the
> argument by value.

And please replace "a reference-type" above with "an rvalue." :)

Ali

> [1]: https://dlang.org/spec/template.html#auto-ref-parameters


May 04, 2020
On 2020-05-03 21:59:54 +0000, Harry Gillanders said:

> I'm unsure as to which part is unclear,

Well, I was trapped by this formatting/syntax:

	size_t drawableCharacterCount (CodePoints) (auto ref CodePoints codePoints)
	if (isInputRange!CodePoints && is(ElementType!CodePoints : dchar))
	{

and was wondering what the first line contributes... now understanding that it's the function signature and the following "if" line is template contraint (IIRC). So it's actually:

	size_t drawableCharacterCount (CodePoints) (auto ref CodePoints codePoints)
		if (isInputRange!CodePoints && is(ElementType!CodePoints : dchar))
	{

However, I find this sytax a bit unfortunate because I can't spot quickly that this "if" is a tempalte constraint... but maybe I get more used to it.

-- 
Robert M. Münch
http://www.saphirion.com
smarter | better | faster