# Count the bytes of a program

Note 2: I accepted @DigitalTrauma's 6-byte long answer. If anyone can beat that I will change the accepted answer. Thanks for playing!

Note: I will be accepting an answer at 6:00pm MST on 10/14/15. Thanks to all that participated!

I am very surprised that this has not been asked yet (or I didn't search hard enough). Either way, this challenge is very simple:

Input: A program in the form of a string. Additionally, the input may or may not contain:

• Trailing newlines
• Non-ASCII characters

Output: Two integers, one representing UTF-8 character count and one representing byte count, you may choose which order. Trailing newlines are allowed. Output can be to STDOUT or returned from a function. IT can be in any format as long as the two numbers are distinguishable from each other (2327 is not valid output).

Notes:

• You may consider newline as \n or \r\n.
• Here is a nice byte & character counter for your tests. Also, here is a meta post with the same thing (Thanks to @Zereges).

Sample I/O: (All outputs are in the form {characters} {bytes})

Input: void p(int n){System.out.print(n+5);}

Output: 37 37

Input: (~R∊R∘.×R)/R←1↓ιR

Output: 17 27

Input:

friends = ['john', 'pat', 'gary', 'michael']
for i, name in enumerate(friends):
print "iteration {iteration} is {name}".format(iteration=i, name=name)

Output: 156 156

This is code golf - shortest code in bytes wins!

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

• does the output have to be space-separated? – Maltysen Oct 14 '15 at 3:24
• no, it can be in any format as long as the numbers are distinguishable from each other (2327 is not valid output) – GamrCorps Oct 14 '15 at 3:25
• Aren't there some UTF-8 characters that depending on the interpretation can be split into two other characters that generate the same byte values? How do we count those then? – Patrick Roberts Oct 14 '15 at 3:51
• Honestly, I do not know what you mean. Therefore, count as you wish. – GamrCorps Oct 14 '15 at 3:52
• @GamrCorps UTF-8 characters include non-ASCII characters, which are basically characters that cannot be represented by one byte but must be represented by two or even four bytes. Depending on how the characters are read in by a program, it is up to the program to choose how to interpret the stream of bytes. For example, a 2 byte UTF-8 can be interpreted as 2 sequential ASCII characters each of which are represented by the two bytes making up the originally intended character. – Patrick Roberts Oct 14 '15 at 3:56

# Shell + coreutils, 6

This answer becomes invalid if an encoding other than UTF-8 is used.

wc -mc

### Test output:

$printf '%s' "(~R∊R∘.×R)/R←1↓ιR" | ./count.sh 17 27$

In case the output format is strictly enforced (just one space separating the the two integers), then we can do this:

# Shell + coreutils, 12

echowc -mc

# C, 68 67 bytes

b,c;main(t){for(;t=~getchar();b++)c+=2!=~t/64;printf("%d %d",c,b);}

This uses the same idea as my other answer.

Try it online on Ideone.

# R, 47 bytes

a<-commandArgs(TRUE);nchar(a,"c");nchar(a,"b")

Input: (~R∊R∘.×R)/R←1↓ιR

Output:

[1] 17
[2] 27

If printing line numbers alongside output isn't allowable under the "any format" then cat can fix the issue:

# R, 52 bytes

a<-commandArgs(TRUE);cat(nchar(a,"c"),nchar(a,"b"))

Input: (~R∊R∘.×R)/R←1↓ιR

Output: 17 27

• As a function, 39 bytes: function(s)c(nchar(s,"c"),nchar(s,"b")) – Alex A. Feb 5 '16 at 6:52
• Also just some general R golfing tips: You can use T in place of TRUE, = in place of <-, and input can come from scan, readline, or function, all of which are shorter than commandArgs. – Alex A. Feb 5 '16 at 6:56

# Milky Way 1.6.2, 7 bytes (non-competing)

':y!^P!

### Explanation

'         read input from the command line
:        duplicate the TOS
y       push the length of the TOS
!  !   output the TOS
^     pop the TOS
P    push the length of the TOS in bytes

### Usage

./mw <path-to-code> -i <input>
• I marked this as non-competing since the challenge predates the language. – Mego Jan 5 '16 at 7:51

$x=get;say$x.chars," ",$x.codes; Based on this blog post at Perl6Advent. # Brainfuck, 163 bytes ,[>+<,]>[>>+>+<<<-]>>>[<<<+>>>-]<<+>[<->[>++++++++++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]++++++++[<++++++>-]>[<<+>>-]>[<<+>>-]<<]>]<[->>++++++++[<++++++>-]]<[.[-]<]< With linebreaks for readability: ,[>+<,]> [>>+>+<<<-]>>>[<<<+>>>-]<<+>[<->[ >++++++++++<[->-[>+>>]>[+[-<+>]>. +>>]<<<<<]>[-]++++++++[<++++++>- ]>[<<+>>-]>[<<+>>-]<<]>]<[->>+++++ +++[<++++++>-]]<[.[-]<]< The most important part is the first line. This counts the number of characters inputted. The rest is just the long junk required to print a number greater than 9. EDIT: Since BF cannot input/output anything but ASCII numbers from 1-255, there would be no way to measure the UTF-8 chars. • This looks like it could be golfed more. But it probably can't. +1. – wizzwizz4 Mar 23 '16 at 17:47 # beeswax, 99 87 bytes A more compact version, 12 bytes shorter than the first: p~5~q")~4~p")~7~g?< >)'qq>@PPq>@Pp>Ag'd@{ >@PPPq @dNp"?{gAV_ >@PPPP>@>?b>N{; The same, as easier to follow hexagonal layout: p ~ 5 ~ q " ) ~ 4 ~ p " ) ~ 7 ~ g ? < > ) ' q q > @ P P q > @ P p > A g ' d @ { > @ P P P q @ d N p " ? { g A V _ > @ P P P P > @ > ? b > N { ; Output as characters, then bytecount, separated by a newline. Example: the small letter s at the beginning of the line just tells the user that the program wants a string as input. julia> beeswax("utf8bytecount.bswx") s(~R∊R∘.×R)/R←1↓ιR 17 27 Program finished! Empty string example: julia> beeswax("utf8bytecount.bswx") s 0 0 Program finished! Beeswax pushes the characters of a string that’s entered at STDIN onto the global stack, coded as the values of their Unicode code points. For easier understanding, here is the unwrapped version of the program above: >@{; >@P@p >@PP@p>@P p _VAg{?"pN>Ag"d?g~7~)"d~4~)"d~5~)"d@PPp ;{N< d? < < @PP< For this example, the character α is entered at STDIN (code point U+03B1, decimal:945) gstack lstack _VA [945,1]• [0,0,0]• enter string, push stack length on top of gstack g [0,0,1]• push gstack top value on top of local stack (lstack) { lstack 1st value to STDOUT (num. of characters) ? [945]• pop gstack top value " skip next if lstack 1st >0 N> print newline, redirect to right Ag [945,1]• [0,0,1]• push gstack length on top of gstack, push that value on lstack. " skip if lstack 1st > 0 ? [945]• pop gstack top value g [0,0,945]• push gstack top value on lstack ~ [0,945,0]• flip lstack 1st and 2nd 7 [0,945,7]• lstack 1st=7 ~ [0,7,945]• flip lstack 1st and 2nd ) [0,7,7]• lstack 1st = lstack 1st >>> 2nd (LSR by 7) " skip next if top >0 ~4~) [0,0,0]• flip,1st=4,flip,LSR by 4 "d skip next if top >0... redirect to upper right >@ redirect to right, flip lstack 1st and 3rd PP@ [2,0,0]• increment lstack 1st twice, flip 1st and 3rd p redirect to lower left " (ignored instruction, not relevant) d? < < []• redirect to left... pop gstack, redirect to upper right >Ag"d [0]• [2,0,0]• redir. right, push gstack length on gstack push gstack top on lstack, skip next if lstack 1st > 0 redir. to upper right. >@ [0,0,2]• redir right, flip lstack 1st/3rd {; output lstack 1st to STDOUT, terminate program Basically, this program checks each codepoint value for the 1-byte, 2-byte, 3-byte and 4-byte codepoint limits. If n is the codepoint value, then these limits for proper UTF-8 strings are: codepoint 0...127 1-byte: n>>>7 = 0 128...2047 2-byte: n>>>11= 0 → n>>>7>>>4 2048...65535 3-byte: n>>>16= 0 → n>>>7>>>4>>>5 65535...1114111 4-byte: the 3 byte check result is >0 You can find the numbers 7,4 and 5 for the shift instructions in the code above. If a check results in 0, the lstack counter is incremented appropriately to tally the number of bytes of the entered string. The @PP...@ constructs increment the byte counter. After each tally, the topmost Unicode point is popped from the gstack until it is empty. Then the byte count is output to STDOUT and the program terminated. There are no checks for improper encoding like overlong ASCII encoding and illegal code points beyond 0x10FFFF, but I think that’s fine ;) # Swift 3, 37 {($0.characters.count,$0.utf8.count)} // where$0 is String

## Usage

Test

{($0.characters.count,$0.utf8.count)}("Hello, world")