New Red release in sight! A try to help!

A new release of the Red programming language is in the making. Many improvements are added as always.

With this post I will try to push the cart a little on in the right direction.

After I have made ENTAB and DETAB native functions, that wait on publishing to the mainline I wanted to write a blog article about how I did that. But shortly after I saw a similar task on the Trello Board. The functions ENLINE and DELINE. Seems also pretty straightforward.

What is enline/deline? Let’s look at the help.

ENLINE:
R3:
USAGE:
ENLINE series

DESCRIPTION:
Converts string terminators to native OS format, e.g. LF to CRLF.
ENLINE is a native value.

ARGUMENTS:
series — (modified) (any-string! block!)

R2:
USAGE:
ENLINE series /with end-of-line

DESCRIPTION:
Converts standard string terminators to current OS format, e.g. LF to CRLF. (Modifies)
ENLINE is a function value.

ARGUMENTS:
series -- (Type: any-string block)

REFINEMENTS:
/with -- Specifies alternate line termination.
end-of-line -- (Type: char string)

DELINE:
R3:
USAGE:
DELINE string /lines

DESCRIPTION:
Converts string terminators to standard format, e.g. CRLF to LF.
DELINE is a native value.

ARGUMENTS:
string -- (modified) (any-string!)

REFINEMENTS:
/lines -- Return block of lines (works for LF, CR, CR-LF endings) (no modify)

R2:
USAGE:
DELINE string /lines

DESCRIPTION:
Converts string terminators to standard format, e.g. CRLF to LF. (Modifies)
DELINE is a function value.

ARGUMENTS:
string -- (Type: any-string)

REFINEMENTS:
/lines -- Convert to block of lines (does not modify)

Does not appear to be too difficult?
Enline:

  1. get the OS, so we know what line terminator we want in the end
  2. replace/all line terminators to OS-line terminator

Deline

  1. get the OS line terminator
  2. change all OS line terminators to standard line terminator.

Next step.

Let’s peek at how it is done in R3 source?

The natives are found here.
But these are more like functions to separate the handling of ASCII and unicode strings, calling other functions in turn.
The functions that we are really looking for are here.

When we look closely we see that the UNI and BYTE functions are practically the same except for the type of characters used.
Because Red handles these differences internally within the string manipulating functions we will not need two different versions.

Functions in string.reds that are candidate functions we can use

append-char
overwrite-char
insert-char
remove-char
poke-char
find (an action in string.reds)

There is also some information in the Rebol cookbook.

Here we learn the line terminators of OS for free

  • CR-LF on Windows systems
  • LF on Unix, Linux, and other Posix systems
  • CR on Macintosh

We sum up the changes deline and enline must make.
deline:
to standard, the lf
- LF -> LF
- CR -> LF
- CR LF -> LF

enline:
on Windows
- LF -> CR LF
- CR -> CR LF
- CR LF -> CR LF

on Posix
- LF -> LF
- CR -> LF
- CR LF -> LF

(This is same as DELINE)

on Mac
- CR -> CR
- LF -> CR
- CR LF -> CR

(This is same as DELINE but with CR as target)

Perhaps we must do things a little different than Rebol does, improve.

And what about the refinements?
If I look at it, DELINE should come with a /with refinement that has a default of lf and it should take cr too.
The refinement /lines for DELINE is a troublemaker. Perhaps split is better for such a case?

Is it really needed to support a /with refinement for ENLINE?

Will you let me know if you like this
To be continued… ?

This entry was posted in Programmeren. Bookmark the permalink.

Geef een reactie

Your email address will not be published. Required fields are marked *

Vul dit nog even in: * Time limit is exhausted. Please reload CAPTCHA.