[Question]
  [
### Task
Given a UTF-8 string (by any means) answer (by any means) an equivalent list where every element is the number of bytes used to encode the corresponding input character.
### Examples
`!` ‚Üí `1`
`Ciao` ‚Üí `1 1 1 1`
`t Éa ä` ‚Üí `1 2 1 2`
`Ad√°m` ‚Üí `1 1 2 1`
`ƒâa≈≠` ‚Üí `2 1 2` (single characters)
`cÃÇauÃÜ` ‚Üí `1 2 1 1 2` (uses combining overlays)
`„ÉÅ„É£„Ç™` ‚Üí `3 3 3`
 (empty input) ‚Üí  (empty output)
`!¬±‚â°©∏Ω` ‚Üí `1 2 3 4`
 (a null byte) ‚Üí `1`
### Null bytes
If the only way to keep reading input beyond null bytes is by knowing the total byte count, you may get the byte count by any means (even user input).
If your language cannot handle null bytes at all, you may assume the input does not contain nulls.
  
[Answer]
## Python 3, ~~42~~ 36 bytes
```
lambda x:[len(i.encode())for i in x]
```
[Answer]
# C, ~~68~~ 65 bytes
```
b;main(c){for(;~c;b=c/64^2?b?putchar(b+48)/48:1:b+1)c=getchar();}
```
*Thanks to @FryAmTheEggman for golfing off 3 bytes!*
Test it on [Ideone](http://ideone.com/RaDWEf).
[Answer]
## APL, 15 chars
```
‚â¢¬®'UTF-8'‚àò‚éïucs¬®
```
In English: convert each character to UTF-8 (meaning: vector of bytes representation) and get its tally.
[Answer]
# Pyth, ~~9~~ 7 bytes
Thanks to @Maltysen for saving 2 bytes!
```
mlc.Bd8
```
[Test suite](http://pyth.herokuapp.com/?code=mlc.Bd8&input=%22t%CA%83a%CA%8A%22&test_suite=1&test_suite_input=%22%21%22%0A%22Ciao%22%0A%22t%CA%83a%CA%8A%22%0A%22Ad%C3%A1m%22%0A%22%C4%89a%C5%AD%22%0A%22%E3%83%81%E3%83%A3%E3%82%AA%22%0A%22%22%0A%22%21%C2%B1%E2%89%A1%F0%A9%B8%BD%22&debug=0)
Converts every character of the input to it's binary representation and then splits this into chunks of length 8. The number of those chunks is then the amount of bytes needed to encode that character.
[Answer]
# GolfScript, 16 bytes
```
{64/2=}%1,/{,)}*
```
[Try it online!](http://golfscript.tryitonline.net/#code=ezY0LzI9fSUxLC97LCl9Kg&input=IcKx4omh8Km4vQ)
### Background
GolfScript doesn't have a clue what Unicode is; all strings (input, output, internal) are composed of bytes. While that can be pretty annoying, it's perfect for this challenge.
UTF-8 encodes ASCII and non-ASCII characters differently:
* All code points below 128 are encoded as `0xxxxxxx`.
* All other code points are encoded as `11xxxxxx 10xxxxxx ... 10xxxxxx`.
This means that the encoding of each Unicode character contains either a single `0xxxxxxx` byte or a single `11xxxxxx` byte and 1 to 5 `10xxxxxx` bytes.
By dividing all bytes of the input by **64**, we turn `0xxxxxxx` into **0** or **1**, `11xxxxxx` into **3**, and `10xxxxxx` into **2**.
If we compare the quotient with **2** – pushing **1** for **2**; and **0** for **0**, **1**, and **3** – each character will be turned into a **0**, followed by 1 to 5 **1**'s.
All that's left is to split the resulting string at occurrences of **0**, count the number of **1**'s between those zeroes and add one to the amount.
### How it works
```
{     }%          Map the following over all bytes in the input.
 64/                Divide the byte by 64.
    2=              Compare the quotient with 2, pushing 1 or 0.
        1,        Push range(1), i.e., [0].
          /       Split the array of Booleans around zeroes.
           {  }*  Fold; for each run of ones but the first:
            ,       Push its length.
             )      Increment.
```
[Answer]
## PowerShell v4, 58 bytes
```
[char[]]$args[0]|%{[Text.Encoding]::UTF8.GetByteCount($_)}
```
---
### NB
OK, this should work, and does in almost all of the test cases except for `©∏Ω` which is somehow counted as `3,3` on my machine. That character even shows as [7 bytes](https://i.stack.imgur.com/mLqdp.png) on my computer. I suspect this is due to some sort of bug in the Windows or .NET version that I'm running locally, as @Mego [doesn't have that issue](https://i.stack.imgur.com/w7mxE.png). (*Edit: @cat points out this is due to [BOM](https://en.wikipedia.org/wiki/Byte_order_mark). Thanks for solving that mystery, @cat!*)
However, that still doesn't account for all of the problem. I think I know where *some* of the problems are coming from, though. Inside .NET, all strings are composed of [UTF-16 code units](https://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx) (which is the System.Char type). With the very loose typecasting that PowerShell uses, there's a *lot* of implicit casting and conversion between types in the background. *Likely* this is a contributing factor to the behavior we're seeing -- for example, `[system.text.encoding]::utf8.getchars([System.Text.UTF8Encoding]::UTF8.GetBytes('©∏Ω'))` returns two unprintables, rather than a single character.
---
### Explanation
Very straightforward code. Takes the input `$args[0]` and explicitly casts it as a char-array so we can loop through each component of the string `|%{...}`. Each iteration, we use the .NET call `[System.Text.Encoding]::UTF8.GetByteCount()` (the `System.` is implied) to get the byte count of the current character `$_`. That's placed on the pipeline for later output. Since it's a collection of `[int]`s that are returned, casting to an array is implicit.
### Test Runs
```
PS C:\Tools\Scripts\golfing> .\bytes-per-character.ps1 't Éa ä'
1
2
1
2
PS C:\Tools\Scripts\golfing> .\bytes-per-character.ps1 'Ad√°m'
1
1
2
1
PS C:\Tools\Scripts\golfing> .\bytes-per-character.ps1 'ƒâa≈≠'
2
1
2
PS C:\Tools\Scripts\golfing> .\bytes-per-character.ps1 'cÃÇauÃÜ'
1
2
1
1
2
PS C:\Tools\Scripts\golfing> .\bytes-per-character.ps1 '„ÉÅ„É£„Ç™'
3
3
3
PS C:\Tools\Scripts\golfing> .\bytes-per-character.ps1 '!¬±‚â°©∏Ω'
1
2
3
3
3
```
*Edited to add* This does properly account for the null-bytes requirement that was added to the challenge after I originally posted, provided you pull the data from a text file and pipe it as follows:
```
PS C:\Tools\Scripts\golfing> gc .\z.txt -Encoding UTF8|%{.\bytes-per-character.ps1 $_}
2
1
1
1
```
[![z.txt](https://i.stack.imgur.com/SgRXR.png)](https://i.stack.imgur.com/SgRXR.png)
[Answer]
## JavaScript (ES6), ~~54~~ ~~45~~ 43 bytes
```
s=>[...s].map(c=>encodeURI(c).length/3-8&7)
```
Edit: Saved 2 bytes with help from @l4m2.
[Answer]
# Ruby, 33 bytes
Barely edges out Python, yay! [Try it online.](https://repl.it/C5cN)
```
->s{s.chars.map{|c|c.bytes.size}}
```
[Answer]
# [Perl 6](http://perl6.org), ~~¬†77 69¬†~~ 63 bytes
```
put +$0 if $_¬ª.base(2).fmt("%8d")~~/^(1)**2..*|^(" ")/ while $_=$*IN.read: 1
```
```
put +$0 if $_¬ª.fmt("%8b")~~/^(1)**2..*|^(" ")/ while $_=$*IN.read: 1
```
```
put 1+$0 if $_¬ª.fmt("%8b")~~/^1(1)+|^" "/while $_=$*IN.read: 1
```
```
put 1+$0 if $_¬ª.fmt("%0.8b")~~/^1(1)+|^0/while $_=$*IN.read: 1
```
Since [Perl¬†6](http://perl6.org) uses NFG strings I have to pull in the bytes directly, which sidesteps the feature.  
(NFG is like NFC except it also creates synthetic composed codepoints)
The output is separated by newlines.
### Test:
```
for text in '!' 'Ciao' 't Éa ä' 'Ad√°m' 'ƒâa≈≠' 'cÃÇauÃÜ' '„ÉÅ„É£„Ç™' '' '!¬±‚â°©∏Ω' '©∏Ω\0©∏Ω';
do
  echo -en $text |
  perl6 -e 'put 1+$0 if $_¬ª.fmt("%8b")~~/^1(1)+|^" "/while $_=$*IN.read: 1' |
  # combine all of the lines into a single one for display purposes
  env text=$text perl6 -e 'put qq["%*ENV<text>"], "\t\t", lines.gist'
done
```
```
"!"     (1)
"t Éa ä"      (1 2 1 2)
"Ad√°m"      (1 1 2 1)
"ƒâa≈≠"       (2 1 2)
"cÃÇauÃÜ"     (1 2 1 1 2)
"„ÉÅ„É£„Ç™"       (3 3 3)
""      ()
"!¬±‚â°©∏Ω"     (1 2 3 4)
"©∏Ω\0©∏Ω"        (4 1 4)
```
### Explanation:
```
# turns the list in ÔΩ¢$0ÔΩ£ into a count, and adds one
# ÔΩ¢putÔΩ£ prints that with a trailing newline
put 1+$0 
   # if the following is true
   if
       # format the input byte to base 2 and pad it out to 8 characters
       $_¬ª.fmt("%8b")
       ~~ # smart match against
       # check to see if it starts with more than one 1s, or a space
       # ( also sets ÔΩ¢$0ÔΩ£ to a list that is 1 shorter
       # than the number of bytes in this codepoint )
       / ^1 (1)+ | ^" " /
           # for every byte in STDIN
           while
               $_ = $*IN.read: 1
```
This works because the first byte in a multi-byte codepoint has the number of bytes encoded inside of it, and the other bytes in the codepoint have the highest bit set, but not the next highest. While the single byte codepoints don't have the highest bit set.
[Answer]
# Python 3, 82 bytes
```
import math
lambda x:[ord(i)<128and 1or int((math.log2(ord(i))-1)//5+1)for i in x]
```
This is much longer than the other Python answer, and the majority of the other answers, but uses an approach involving logarithms that I haven't yet seen.
An anonymous function that takes input, via argument, as a string and returns a list.
[Try it on Ideone](https://ideone.com/p0p6QU)
**How it works**
This method relies on the way in which UTF-8 encodes the code-point of a character. If the code-point is less than 128, the character is encoded as in ASCII:
```
0xxxxxxx
```
where `x` represents the bits of the code point. However, for code-points greater than or equal to 128, the first byte is padded with the same number of `1` s as the total number of bytes, and subsequent bytes begin `10`. The bits of the code-point are then entered to give the shortest possible multibyte sequence, and any remaining bits become `0`.
```
No. of bytes  Format
1             0xxxxxxx
2             110xxxxx 10xxxxxx
3             1110xxxx 10xxxxxx 10xxxxxx
4             11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
...           ...
```
and so forth.
It can now be noticed that for each number of bytes `n`, the upper limit for the number of code-point bits is given by `(-n+7)+6(n-1) = 5n+1`. Hence, the upper limit code-point `c` for each `n` is given, in decimal, by  `c= 2^(5n+1)`. Rearranging this gives `n = (log2(c)-1)/5`. So for any code-point, the number of bytes can be found by evaluating the above expression, and then taking the ceiling.
However, this does not work for code points in the range `64 <= c <= 127`, since the lack of a padding `1` due to the ASCII-like encoding for 1 byte characters means that the wrong upper limit is predicted, and `log2` is undefined for `c = 0`, which happens if a null byte is present in the input. Therefore, if `c <= 127`, a value of `1` is returned for n.
This is exactly what the code is doing; for each character `i` in the string `x`, the code-point is found using the `ord` function, and the ceiling of the expression is found by using integer rather than float division by `5` and then adding `1`. Since Python's float type always represents integers as `x.0`, even after integer division, the result is passed to the `int` function to remove the trailing zero. If `ord(i) <= 127`, logical short-circuiting means that `1` is instead returned. The number of bytes for each character is stored as an element in a list, and this list is returned.
[Answer]
# Java 10, ~~100~~ ~~96~~ ~~95~~ ~~67~~ 61 bytes
```
a->{for(var c:a)System.out.print(c.getBytes("utf8").length);}
```
-4 bytes removing spaces because [this is allowed in the comments](https://codegolf.stackexchange.com/questions/83673/bytes-character/83690#comment204264_83673)  
-1 byte changing `UTF-8` to `utf8`  
**Explanation:**
[Try it online.](https://tio.run/##lZE7TsNAEIZ7n2Lial3ESomwQAJESZqUkGLYrJ0N9tryjhOiyAWJhBRuEVGBREFHk4ZHE@UU@ALcwDjBokAU2WaleXz/fNIOcIjNQe@q5CFqDWco1cQCkIpE6iMX0N6UAMNY9oCzDqVSBedddID6aTzScHrNRUIyVl61l1vVowlJcmiDggMosXk48eOUDTEFvo9OZ6xJRG6ckZtUWcS4Gwg6HpPQzM7I37MdNxQqoL7j5aW3CUyyy7AKrHO3JlHl@SsD/9j8WCuXM7thuzoJJTHbdhxv2/8rESpWT7bEicTYGKLVDFd3xthR720RGVPvc/x4Mqb4eorZ@taYK2Y3xey@mD6akkqMoP6lVne3W43X58/54uvhZWmsedHaHcmtvPwG)
```
a->{                      // Method with String-array parameter and no return-type
  for(var c:a)            //  Loop over the input-array
    System.out.print(     //   Print:
      c.getBytes("utf8")  //    The bytes as array in UTF-8 of the current item,
       .length);}         //    and print the amount of bytes in this array
```
[Answer]
# Julia, 34 bytes
```
s->s>""?map(sizeof,split(s,"")):[]
```
This is an anonymous function that accepts a string and returns an integer array. To call it, assign it to a variable.
The approach is quite straightforward: If the input is empty, the output is empty. Otherwise we map the `sizeof` function, which counts the number of bytes in a string, to each one-character substring.
[Try it online!](http://julia.tryitonline.net/#code=Zj1zLT5zPiIiP21hcChzaXplb2Ysc3BsaXQocywiIikpOltdCgpmb3IgdGVzdCBpbiBbKCIhIiwgWzFdKSwKICAgICAgICAgICAgICgiQ2lhbyIsIFsxLDEsMSwxXSksCiAgICAgICAgICAgICAoInTKg2HKiiIsIFsxLDIsMSwyXSksCiAgICAgICAgICAgICAoIkFkw6FtIiwgWzEsMSwyLDFdKSwKICAgICAgICAgICAgICgixIlhxa0iLCBbMiwxLDJdKSwgICAgICAjIHNpbmdsZSBjaGFyYWN0ZXJzCiAgICAgICAgICAgICAoImPMgmF1zIYiLCBbMSwyLDEsMSwyXSksICAjIHVzZXMgY29tYmluaW5nIG92ZXJsYXlzCiAgICAgICAgICAgICAoIuODgeODo-OCqiIsIFszLDMsM10pLAogICAgICAgICAgICAgKCIiLCBbXSksCiAgICAgICAgICAgICAoIiHCseKJofCpuL0iLCBbMSwyLDMsNF0pXQogICAgcHJpbnRsbihmKHRlc3RbMV0pID09IHRlc3RbMl0pCmVuZA&input=) (includes all test cases)
[Answer]
# PHP, ~~92~~ 57 bytes
On second thought you can do this with much less faffing around:
```
<?php for(;$a=strlen(mb_substr($argv[1],$i++,1));)echo$a;
```
[Try it online](https://ideone.com/Z7qsFj) note that this is slightly longer as it uses stdin rather than a program argument.  
This version requires you to ignore notices sent to stderr but [that's fine](http://meta.codegolf.stackexchange.com/questions/4780/should-submissions-be-allowed-to-exit-with-an-error/).
old version:  
Uses a rather different approach to the other php answer.
Relies on the lack of native support for multi-byte strings in php.
```
<?php for($l=strlen($a=$argv[1]);$a=mb_substr($a,1);$l=$v)echo$l-($v=strlen($a));echo$l?:'';
```
[Answer]
# Emacs Lisp, ~~55~~ 49 bytes
```
(lambda(s)(mapcar'string-bytes(mapcar'string s)))
```
First dissects the string into a list of characters with `(mapcar 'string s)`. The `string` function in Emacs Lisp takes a list of characters and builds a string out of them. Due to the way Emacs splits strings with `mapcar` (i.e. into a list of integers, not characters or strings), this explicit conversion is needed. Then maps the `string-bytes` function onto that list of strings.
Example:
```
(mapcar 'string "abc") ; => ("a" "b" "c")
(mapcar 'string-bytes '("a" "b" "c")) ; => (1 1 1) 
```
Testcases:
```
(mapcar
 (lambda(s)(mapcar'string-bytes(mapcar'string s)))
 '("!""Ciao""t Éa ä""Ad√°m""ƒâa≈≠""cÃÇauÃÜ""„ÉÅ„É£„Ç™""""!¬±‚â°©∏Ω""\0"))
;; ((1) (1 1 1 1) (1 2 1 2) (1 1 2 1) (2 1 2) (1 2 1 1 2) (3 3 3) nil (1 2 3 4) (1))
```
~~Old answer:~~
```
(lambda(s)(mapcar(lambda(s)(string-bytes(string s)))s))
```
Ungolfed:
```
 (lambda (s)
   (mapcar
    ;; we can't use string-bytes directly,
    ;; since Emacs mapcar yields a list of ints instead of characters
    ;; therefore we need a wrapper function here. 
    (lambda (s)
      (string-bytes (string s)))
    s))
```
Testcases:
```
(mapcar
 (lambda(s)(mapcar(lambda(s)(string-bytes(string s)))s))
 '("!""Ciao""t Éa ä""Ad√°m""ƒâa≈≠""cÃÇauÃÜ""„ÉÅ„É£„Ç™""""!¬±‚â°©∏Ω""\0"))
;; ((1) (1 1 1 1) (1 2 1 2) (1 1 2 1) (2 1 2) (1 2 1 1 2) (3 3 3) nil (1 2 3 4) (1))
```
[Answer]
# JavaScript (Node), 27 bytes
```
s=>s.map(Buffer.byteLength)
```
This takes input as an array of individual characters, and returns an array of byte counts.
`Buffer` is a method of representing raw binary data. [Buffer.byteLength(string)](https://nodejs.org/api/buffer.html#buffer_class_method_buffer_bytelength_string_encoding) gives the number of bytes in the string. UTF-8 is the default encoding. Note that only Node.js has buffers, not browser JS. The rough browser equivalent is called [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob), which comes in at 31 bytes:
```
s=>s.map(e=>new Blob([e]).size)
```
## Test
Save this file and run it through node, or [try it online](https://repl.it/C546).
```
var f =
  s=>s.map(Buffer.byteLength)
var tests = [
  ["!"],
  ["C","i","a","o"],
  ["t"," É","a"," ä"],
  ["A","d","√°","m"],
  ["ƒâ","a","≈≠"],
  ["c","ÃÇ","a","u","ÃÜ"],
  ["„ÉÅ","„É£","„Ç™"],
  [],
  ["!","¬±","‚â°","©∏Ω"]
];
tests.forEach(test => {
  console.log(test, f(test));
});
```
This should be the result:
```
$ node bytes.js
[ '!' ] [ 1 ]
[ 'C', 'i', 'a', 'o' ] [ 1, 1, 1, 1 ]
[ 't', ' É', 'a', ' ä' ] [ 1, 2, 1, 2 ]
[ 'A', 'd', '√°', 'm' ] [ 1, 1, 2, 1 ]
[ 'ƒâ', 'a', '≈≠' ] [ 2, 1, 2 ]
[ 'c', 'ÃÇ', 'a', 'u', 'ÃÜ' ] [ 1, 2, 1, 1, 2 ]
[ '„ÉÅ', '„É£', '„Ç™' ] [ 3, 3, 3 ]
[] []
[ '!', '¬±', '‚â°', 'ÔøΩ' ] [ 1, 2, 3, 4 ]
```
[Answer]
# Bash, 74 bytes
**Golfed**
```
xxd -p|fold -2|cut -c1|tr -d '89ab'|echo `tr -t '01234567cbef' '[1*]2234'`
```
**Algorithm**
hexdump input string, fold 2 chars per line, cut the first char only
```
echo -ne '!¬±‚â°©∏Ω' | xxd -p|fold -2|cut -c1
2
c
b
e
8
a
f
a
b
b
```
(4 high order bits of an each input byte as a hex char, one per line)
Remove "continuation bytes" 0x80..0xBF
```
tr -d '89ab'
2
c
e
f
```
(what is left, is 4 bits of the first byte of an each unicode char)
map the first bits into the char length, collapse the output and print
```
echo `tr -t '01234567cbef' '[1*]2234'`
1 2 3 4
```
**Test**
```
 U() { xxd -p|fold -2|cut -c1|tr -d '89ab'|echo `tr -t '01234567cbef' '[1*]2234'`;}
 echo -ne '!' | U 
 1
 echo -ne 'Ciao' | U
 1 1 1 1
 echo -ne 't Éa ä' | U
 1 2 1 2
 echo -ne 'Ad√°m' | U
 1 1 2 1
 echo -ne 'ƒâa≈≠' | U
 2 1 2
 echo -ne 'cÃÇauÃÜ' | U
 1 2 1 1 2
 echo -ne '„ÉÅ„É£„Ç™' | U
 3 3 3
 echo -ne '!¬±‚â°©∏Ω' | U
 1 2 3 4
 echo -ne "\x0" | U
 1
 echo -ne '' | U
```
[Answer]
# PHP, 126 bytes
```
<?php $s=fgets(STDIN);echo $s!=''?implode(' ',array_map(function($x){return strlen($x);},preg_split('/(?<!^)(?!$)/u',$s))):'';
```
[Try it online!](https://ideone.com/PY6B7a)
[Answer]
# C#, ~~89~~ 82 bytes
```
I=>{var J="";foreach(char c in I){J+=Encoding.UTF8.GetByteCount(c+"");}return J;};
```
A simple C# lambda that iterates through the string and returns the space separated list.
**Edit:** saved 6 bytes thanks to some very nice comments.
[Answer]
# Haskell, 85 bytes
```
import Data.ByteString as B
import Data.ByteString.UTF8
(B.length.fromString.pure<$>)
```
[Answer]
# Pyth, 17 bytes
```
mhxS+11+16,7lCdlC
```
[Try it online!](http://pyth.herokuapp.com/?code=mhxS%2B11%2B16%2C7lCdlC&input=%22%21%C2%B1%E2%89%A1%F0%A9%B8%BD%22&debug=0)
Use the code-point of the characters with some arithmetics.
[Answer]
# C, 85 bytes.
```
l(unsigned char* c){while(*c){int d=(*c>>4)-11;
d=d<0?1:d+(d==1);putchar(48+d);c+=d;}}
```
Examines the high 4 bits of each byte to determine the encoding and the number of subsequent bytes to skip;
[Answer]
# Factor, ~~57~~ ~~87~~ ~~82~~ 80 bytes
```
[ [ dup zero? [ drop "1"] [ >bin length 4 /i 10 >base ] if ] { } map-as ""join ]
```
Explained:
```
USING: kernel math math.parser sequences ;
IN: byte-counts
: string>byte-counts ( str -- counts )
  [                  ! new quotation: takes a char as a fixnum
    dup zero?        ! true if this is a NUL byte
    [ drop "1" ]     ! NUL bytes have length 1
    [ >bin           ! else, convert to binary string
      length         ! length of binary string
      4              ! the constant 4
      /i             ! integer division
      number>string  ! 4 -> "4"
    ] if             ! conditionally execute one of the previous quotations
  ]                  ! end
  { } map-as         ! map and clone-like an { } array
  "" join ;          ! join array of 1strings on empty string
```
Unit tests:
```
USING: tools.test byte-counts ;
IN: byte-counts.tests
{ "1" } [ "!" string>byte-counts ] unit-test
{ "1111" } [ "Ciao" string>byte-counts ] unit-test
{ "1212"} [ "t Éa ä" string>byte-counts ] unit-test
{ "1121" } [ "Ad√°m" string>byte-counts ] unit-test
{ "212" } [ "ƒâa≈≠" string>byte-counts ] unit-test
{ "12112" } [ "cÃÇauÃÜ" string>byte-counts ] unit-test
{ "333" } [ "„ÉÅ„É£„Ç™" string>byte-counts ] unit-test
{ "" } [ "" string>byte-counts ] unit-test
{ "1234" } [ "!¬±‚â°©∏Ω" string>byte-counts ] unit-test
{ "1" } [ "\0" string>byte-counts ] unit-test
```
They all pass, now. c:
[Answer]
# Swift 2.2, ~~67~~ ~~52~~ 50 bytes
```
for c in i.characters{print(String(c).utf8.count)}
```
Horribly ugly. There's no way to get the UTF-8 length of a Character in Swift, so I need to iterate through the string by character, convert the `Character` to a `String`, and find the `count` of that single-character `String` (hey, at least there's a built-in method to do that). Looking for optimizations, possibly using a scanner.
Revision 1: Saved 15 bytes by using `count` instead of `underestimateCount()`.
Revisions 2: Saved another 2 character by using a for-in loop instead of a for each closure.
[Answer]
## Rust, 53 bytes
```
|s:&str|for c in s.chars(){print!("{}",c.len_utf8())}
```
Rust has utf-8 char primitives, iterators, and lambdas, so this was straightforward. Test code:
```
fn main() {
    let s = "L√∂we ËÄÅËôé L√©opardüíñüíñüíñüíñ";
    let f =|s:&str|for c in s.chars(){print!("{}",c.len_utf8())};
    f(s);
}
```
Outputs
```
1211133112111114444 
```
[Answer]
# jq, 26 characters
(23 characters code + 3 characters command line option)
```
(./"")[]|utf8bytelength
```
Hopefully competing. Although `utf8bytelength` was [added 9++ months before](https://github.com/stedolan/jq/commit/83e8ec587f56a88980c55204ee8433e2f50419cc) this question, it is still not included in released version.
Sample run:
```
bash-4.3$ ./jq -R '(./"")[]|utf8bytelength' <<< 't Éa ä'
1
2
1
2
bash-4.3$ ./jq -R '(./"")[]|utf8bytelength' <<< 'cÃÇauÃÜ '
1
2
1
1
2
1
bash-4.3$ ./jq -R '(./"")[]|utf8bytelength' <<< '„ÉÅ„É£„Ç™'
3
3
3
bash-4.3$ ./jq -R '(./"")[]|utf8bytelength' <<< ''
bash-4.3$ ./jq -R '(./"")[]|utf8bytelength' <<< '!¬±‚â°©∏Ω'
1
2
3
4
```
[Answer]
# [C (gcc)](https://gcc.gnu.org/), 53 bytes
```
k=49;f(char*s){*++s/64-2?k=puts(&k)+47:++k;*s&&f(s);}
```
[Try it online!](https://tio.run/##S9ZNT07@/z/b1sTSOk2jNK84Mz0vNUUhOSOxSKtYs1pLW7tY38xE18g@27agtKRYQy1bU9vE3EpbO9taq1hNLU2jWNO69n9uYmaehmZ1mobSkc7Eo2uVQGIA "C (gcc) ‚Äì Try It Online")
[Answer]
# SmileBASIC, 69 bytes
```
DEF C B
WHILE I<LEN(B)Q=INSTR(BIN$(B[I],8),"0")I=I+Q+!Q?Q+!Q
WEND
END
```
Input is an array of bytes.
The number of bytes in a UTF-8 character is equal to the number of leading `1` bits in the first byte (unless there are no `1`s, in which case the character is 1 byte). To find the number of leading 1s, the program finds the first `0` in the binary representation, then adds 1 if this was 0.
```
0xxxxxxx - no leading ones, 1 byte
110xxxxx 10xxxxxx - 2 leading ones, 2 bytes
1110xxxx 10xxxxxx 10xxxxxx - 3 leading ones, 3 bytes
etc.
```
[Answer]
# F#, ~~59~~ ~~54~~ 66 bytes
```
(s)=seq{for c in s->System.Text.Encoding.UTF8.GetByteCount([|c|])}
```
Technically, s is a char sequence, but it turns out there's an implicit conversion that allows a string to be passed in.
When testing this in the console with `!¬±‚â°©∏Ω`, it splits the kanji into two characters, each 3 bytes long. All the other test cases work fine.
**Edit:** It turns out common namespace imports are not implicit. Up another 12 chars.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 15 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
√áŒµD≈æy‚Äπi1√´.¬≤<5√∑>
```
[**Try it online.**](https://tio.run/##MzBNTDJM/X9u6//D7ee2uhzdV/moYWem4eHVeoc22Zge3m73//D6WK/aQ7v/Rys5ZybmK@koKJWcak481QViOaYcXpgLYhzpTDy6FsRIPtOUWHqmDcR83Nz4uHnx46ZVIA4IKx7a@Khz4YeVO/aCeDEGSrEA)  
Header `Œµ` is used to for-each over all the test cases;  
Footer `√Ø]J]¬ª` to pretty-print the output character-lists (`√Ø`: decimals and characters to integers; `]`: close if-else and for-each; `J`: Join digits together; `}`: close header foreach; `¬ª`: Join by new-lines).
**Explanation:**
```
√á                   # Convert each character to its unicode value
 ŒµD                 # Foreach over this list
      i             #  If the current item
     ‚Äπ              #  is smaller than
   ≈æy               #  128
       1            #   Use 1
        √´           #  Else
         .¬≤         #   Use log_2
           <        #   minus 1
            5√∑      #   integer-divided by 5
              >     #   plus 1
```
Since 05AB1E doesn't have any builtins to convert characters to amount of bytes used, I use `√á` to convert the characters to their unicode values, and in a for-each do the following in pseudo-code:
```
if(unicodeValue < 128)
  return 1
else
  return log_2(unicodeValue-1)//5+1    # (where // is integer-division)
```
Inspired by [*@TheBikingViking*'s Python 3 answer](https://codegolf.stackexchange.com/a/83798/52210).
[Answer]
# [Zsh](https://www.zsh.org/), 41 bytes
```
for c (${(s::)1})set +o multibyte&&<<<$#c
```
[Try it online!](https://tio.run/##qyrO@J@moVn9Py2/SCFZQUOlWqPYykrTsFazOLVEQTtfIbc0pyQzqbIkVU3NxsZGRTn5fy1XmoJ6yanmxFNdj5sb1f8DAA "Zsh ‚Äì Try It Online")
Zsh is UTF-8 aware, so we split the string on characters, then disable multibyte and print each character's length.
]