从字面上看CR和LF

Adám 05/09/2018. 7 answers, 914 views
code-golf string typography

作为对旧记事本的庆祝,我们将把回车和换行视为原来的意思,而不是今天(ab-)使用它们。

给定一个由可打印的ASCII加上换行符(␊; LF; esc \n ; hex 0A; dec 10)和回车符(␍; CR; esc \r ; hex 0D; dec 13)组成的字符串, 导致Try It Online显示如果在打印机上打印出可打印的字符,如何按字面意思取出这两个控制字符:

  1. 在换行时,继续向下打印一行
  2. 在回车上继续从左边缘打印
  3. 多个连续回车符的行为类似于单个回车符

由于现代装置具有过度打击的问题,除了在输入开始时,一个或多个回车的运行将在没有至少一个在前和/或后续换行的情况下发生。 但是,两次回车运行可以用一个换行符分隔。

只要至少保留输入中给出的空白量,任何数量的额外尾随空格都可以接受,无论是在任何行的右侧还是在整个文本的下方。

示例(使用\n\r进行换行和回车)

Lorem ipsum dolor sit amet,

Lorem ipsum dolor sit amet, 

consectetur adipiscing\nelit, sed

consectetur adipiscing
                      elit, sed 

do eiusmod\r\ntempor incididunt\n\n ut labore

do eiusmod
tempor incididunt

                  ut labore 

et dolore\n\rmagna \r\r\naliqua. Ut et dolore\n\rmagna \r\r\naliqua. Ut (注意尾随空格)

et dolore
magna          
aliqua. Ut 

\nenim ad minim veniam,\n\r quis nostrud

   enim ad minim veniam,       quis nostrud 

\rexercitation\r\n\rullamco laboris\n\r\nnisi ut aliquip ex\n\n\rea commodo consequat.\n\n

 实习  ullamco laboris   nisi ut aliquip ex   ea commodo consequat。   

7 Answers


G B 05/09/2018.

Ruby24个 17字节

 ->s{s.tr $/,"\v"} 

在线尝试!

它不适用于TIO,但适用于Linux控制台。


TFeld 05/09/2018.

Python 2,150 128 122 104 103字节

 def f(s):
 i=n=0;l=''
 for c in s:l,n,i=[l,l+c,l+' '*i*n+c,n,1,0,0,i,i+1]['\r\n'.find(c)%3::3]
 print l 

在线尝试!


保存:

  • -1字节,感谢林恩

Kevin Cruijssen 05/09/2018.

Java 10,211 207 206字节

 s->{var a=s.replace("\r\n","\n\r").split("(?<=\n)");int i=0,p=0,j;for(var x:a){for(j=x.charAt(0)<14?0:p;j-->0;x=" "+x);j=(a[i++]=x.replace("\r","")).length()-1;p=x.matches("\\s+")?p:j;}return"".join("",a);} 

在线尝试。

Explanation:

 s->{                      // Method with String as both parameter and return-type
  var a=s.replace("\r\n","\n\r")
                          //  Replace all "\r\n" with "\n\r"
        .split("(?<=\n)");//  Create String-array split by "\n",
                          //  without removing the trailing "\n" delimiter
  int i=0,                //  Index integer
      p=0,                //  Previous line-length, starting at 0
      j;                  //  Temp integer
  for(var x:a){           //  Loop over the String-array
    for(j=x.charAt(0)<14?0//   If the current line starts with either '\r' or '\n':
        0                 //    Prepend no spaces
       :                  //   Else:
        p;j-->0;x=" "+x); //    Prepand `p` amount of spaces for the current item
    j=(a[i++]=x.replace("\r",""))
                          //   Remove all "\r" from the current line
       .length()-1;       //   Set `j` to the current line-length (minus the trailing '\n')
    p=x.matches("\\s+")?  //   If the current line only contains '\r', '\n' and/or spaces:
       p                  //    Leave `p` unchanged
      :                   //   Else:
       j;}                //    Change `p` to this line-length minus 1
  return"".join("",a);}   //  Return the String-array joined together 

挑战前的旧答案改为151 148 bytes

 s->{String a[]=s.replace("\r\n","\n\r").split("(?<=\n)"),r="";int p=0,i;for(var x:a){for(i=p;i-->0;r+=" ");i=x.length()-1;p=i<1?p:i;r+=x;}return r;} 

Explanation:

 s->{                            // Method with String as both parameter and return-type
  String a[]=s.replace("\r\n","\n\r") 
                                //  Replace all "\r\n" with "\n\r"
              .split("(?<=\n)"),//  Create String-array split by "\n",
                                //  without removing the trailing "\n" delimiter
         r="";                  //  Result-String, starting empty
  int p=0,                      //  Previous line-length, starting at 0
      i;                        //  Index (and temp) integer
  for(var x:a){                 //  Loop over the String-array
    for(i=p;i-->0;r+=" ");      //   Append `p` amount of spaces to the result
    i=x.length()-1;p=i<1?p:j;   //   If the current line is not empty:
                                //    Replace `p` with the length of this current line
    r+=x;}                      //   Append the current item
  return r;}                    //  Return the result-String 

不适用于TIO,适用于Windows命令提示符:

在此处输入图像描述


tsh 05/09/2018.

JavaScript(Node.js) ,85个字节

 s=>s[R='replace'](/(.*)([\n\r]+)/g,(_,t,b)=>t+b[R](/\r/g,_=>s='',s=t[R](/./g,' '))+s) 

在线尝试!


Toby Speight 05/09/2018.

C(gcc) ,100个字节

 c;d;f(char*s)NO 

在线尝试! (需要Javascript)

可读版本

 int c = 0;
int d = 0;

f(char*s)
{
    for (;*s;++s) {
        switch (*s) {
        case'\r':
            c = d = 0;
            break;
        case'\n':
            d = c;
            putchar(*s);
            break;
        default:
            printf("%*s%c", d, "", *s);
            d = 0;
            ++c;
        }
    }
} 

c是当前列位置; d是在可打印字符之前必须插入的空格数。 在进入函数时假设两者都为零。

测试程序

 int main(int argc, char **argv)
{
    char s[1024];
    if (argc <= 1)
        while (fgets(s, sizeof s, stdin))
               f(s);
    else
        for (int i = 1;  i < argc;  ++i)
            f(argv[i]);
} 

Neil 05/09/2018.

木炭 ,10个字节

UTFθ«ι≡ι⸿↑ 

在线尝试! 链接是详细版本的代码。 说明:

UT 

禁用右边填充。

Fθ« 

循环输入。

ι 

打印当前字符。 这会自动处理\n (Charcoal在此上下文中将其视为\v )但Charcoal将\r转换为\r\n ,所以...

≡ι⸿ 

...检查\r ...

 

......如果是这样的话,那就回到原点。


ovs 05/09/2018.

Python 3,101 94字节

基于TFeld的回答

 def f(s):
 i=n=0
 for c in s:k='\r\n'.find(c);a=k&1;print(end=-k*' '*i*n+c*a);n=k>0;i=i*a-k//2 

在线尝试!


HighResolutionMusic.com - Download Hi-Res Songs

1 Alan Walker

Diamond Heart flac

Alan Walker. 2018. Writer: Alan Walker;Sophia Somajo;Mood Melodies;James Njie;Thomas Troelsen;Kristoffer Haugan;Edvard Normann;Anders Froen;Gunnar Greve;Yann Bargain;Victor Verpillat;Fredrik Borch Olsen.
2 Sia

I'm Still Here flac

Sia. 2018. Writer: Sia.
3 Little Mix

Woman Like Me flac

Little Mix. 2018. Writer: Nicki Minaj;Steve Mac;Ed Sheeran;Jess Glynne.
4 Cardi B

Taki Taki flac

Cardi B. 2018. Writer: Bava;Juan Vasquez;Vicente Saavedra;Jordan Thorpe;DJ Snake;Ozuna;Cardi B;Selena Gomez.
5 Eminem

Venom flac

Eminem. 2018. Writer: Eminem.
6 Halsey

Without Me flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
7 Lady Gaga

I'll Never Love Again flac

Lady Gaga. 2018. Writer: Benjamin Rice;Lady Gaga.
8 Kelsea Ballerini

This Feeling flac

Kelsea Ballerini. 2018. Writer: Andrew Taggart;Alex Pall;Emily Warren.
9 Bradley Cooper

Shallow flac

Bradley Cooper. 2018. Writer: Andrew Wyatt;Anthony Rossomando;Mark Ronson;Lady Gaga.
10 Dewain Whitmore

Burn Out flac

Dewain Whitmore. 2018. Writer: Dewain Whitmore;Ilsey Juber;Emilio Behr;Martijn Garritsen.
11 Mako

Rise flac

Mako. 2018. Writer: Riot Music Team;Mako;Justin Tranter.
12 Bradley Cooper

Always Remember Us This Way flac

Bradley Cooper. 2018. Writer: Lady Gaga;Dave Cobb.
13 Lukas Graham

Love Someone flac

Lukas Graham. 2018. Writer: Don Stefano;Morten "Rissi" Ristorp;Morten "Pilo" Pilegaard;Jaramye Daniels;James Alan;David LaBrel;Lukas Forchhammer Graham.
14 Avril Lavigne

Head Above Water flac

Avril Lavigne. 2018. Writer: Stephan Moccio;Travis Clark;Avril Lavigne.
15 Deep Chills

Run Free flac

Deep Chills. 2018.
16 Khalid

Better flac

Khalid. 2018. Writer: Charlie Handsome;Jamil Chammas;Denis Kosiak;Tor Erik Hermansen;Mikkel Stoleer Eriksen;Khalid.
17 Charli XCX

1999 flac

Charli XCX. 2018. Writer: Charli XCX;Troye Sivan;Leland;Oscar Holter;Noonie Bao.
18 NCT 127

Regular (English Version) flac

NCT 127. 2018.
19 Camila Cabello

Consequences (Orchestra) flac

Camila Cabello. 2018. Writer: Emily Lynn Weisband;Nicolle Galyon;Amy Wadge;Bart Schoudel;Frank Dukes;Camila Cabello.
20 Lady Gaga

Look What I Found flac

Lady Gaga. 2018. Writer: DJ White Shadow;Nick Monson;Mark Nilan Jr;Lady Gaga.

Related questions

Hot questions

Language

Popular Tags