base/dataurl
"data:" 주소 풀기. (원본은 fetch 와 브라우저가 data: 주소를 그대로 받는다)
- 본문의 퍼센트 꼴을 먼저 풀고, 머리가 ;base64 로 끝나면(대소문자 가리지 않음) base64 를 푼다. (fetch 의 data: 주소 처리와 같은 차례)
- 형은 "data:" 와 ;base64 사이다. 비었으면 fetch 처럼 text/plain;charset=US-ASCII 다.
- 데스크탑 애셋 읽기(Engine::request_asset_bytes, load_image, load_audio_asset)와 타임라인의 data: 그림이 쓴다. 웹은 브라우저 fetch 가 data: 주소를 푼다. (애셋 기준 주소를 앞에 붙이지 않는다)
Decodes a "data:" URL. (in the original, fetch and the browser accept data: URLs as is)
- Decodes percent forms in the body first, then decodes base64 if the header ends with ;base64 (case-insensitive). (the same order as fetch's data: URL handling)
- The type is between "data:" and ;base64. If empty, it is text/plain;charset=US-ASCII like fetch.
- Used by desktop asset loading (Engine::request_asset_bytes, load_image, load_audio_asset) and the timeline's data: images. On the web, the browser's fetch decodes data: URLs. (the asset base URL is not prepended)
DataUrlstruct
푼 data: 주소. (형과 바이트)
A decoded data: URL. (type and bytes)
use vanilla::base::dataurl::parse_data_url;
let data_url = parse_data_url("data:text/plain;base64,SGVsbG8=").unwrap();
assert_eq!(data_url.mime_type, "text/plain");
assert_eq!(data_url.bytes, b"Hello".to_vec());형. ("data:" 와 ;base64 사이 글의 앞뒤 공백을 뗀 것, 대소문자는 그대로. 비었으면 text/plain;charset=US-ASCII)
Type. (the text between "data:" and ;base64 with leading and trailing whitespace trimmed, case kept. text/plain;charset=US-ASCII if empty)
반환ReturnsString
본문을 푼 바이트. (퍼센트 꼴을 풀고, ;base64 면 base64 까지 푼 것)
Decoded bytes of the body. (percent forms decoded, and base64 decoded too if ;base64)
반환ReturnsVec<u8>
함수Functions
data: 주소인지. (앞의 "data:" 를 대소문자 가리지 않고 본다)
Whether it is a data: URL. (checks the leading "data:" case-insensitively)
- location&str
- 볼 주소나 애셋 경로.URL or asset path to check.
반환Returnsbool 앞 5 바이트가 "data:" 이면 true. (5 바이트보다 짧으면 false)true if the first 5 bytes are "data:". (false if shorter than 5 bytes)
data: 주소 풀기. (쉼표가 없거나 base64 가 틀리면 None)
Decodes a data: URL. (None if there is no comma or the base64 is invalid)
- data_url&str
- 풀 data: 주소. (
data:형[;base64],본문)data: URL to decode. (data:type[;base64],body)
반환ReturnsOption<DataUrl> 형과 바이트. data: 주소가 아니거나, 쉼표가 없거나, ;base64 인데 퍼센트 꼴을 푼 본문이 UTF-8 이 아니거나 base64 에 틀린 글자가 있으면 None.Type and bytes. None if it is not a data: URL, has no comma, or is ;base64 and the percent-decoded body is not UTF-8 or the base64 has invalid characters.
use vanilla::base::dataurl::parse_data_url;
let text_url = parse_data_url("data:,a%20b").unwrap();
assert_eq!(text_url.mime_type, "text/plain;charset=US-ASCII");
assert_eq!(text_url.bytes, b"a b".to_vec());
assert!(parse_data_url("data:audio/wav;base64").is_none());
assert!(parse_data_url("data:audio/wav;base64,@@@@").is_none());
assert!(parse_data_url("sounds/hit.wav").is_none());base64 풀기. (표준 알파벳, = 채움과 공백은 건너뛴다. 틀린 글자가 있으면 None)
- URL 안전 알파벳의 - 와 _ 도 + 와 / 처럼 받는다. = 는 자리를 가리지 않고 건너뛰고, 8 비트가 안 되는 끝 비트는 버린다.
Decodes base64. (standard alphabet, = padding and whitespace are skipped. None if there are invalid characters)
- The URL-safe alphabet's - and _ are also accepted like + and /. = is skipped wherever it appears, and trailing bits that do not make 8 bits are discarded.
- text&str
- base64 글. (공백은 스페이스, 탭, 줄바꿈 문자)base64 string. (whitespace is space, tab and newline characters)
반환ReturnsOption<Vec<u8>> 푼 바이트. 알파벳, =, 공백 말고 다른 글자가 있으면 None.Decoded bytes. None if there are characters other than the alphabet, = and whitespace.
use vanilla::base::dataurl::decode_base64;
assert_eq!(decode_base64("SGVsbG8="), Some(b"Hello".to_vec()));
assert_eq!(decode_base64("SGk"), Some(b"Hi".to_vec()));
assert_eq!(decode_base64("S*"), None);퍼센트 꼴 풀기. (%XX 를 그 바이트로 바꾸고, 뒤에 16진 두 자리가 없는 % 는 그대로 둔다)
Decodes percent forms. (replaces %XX with that byte, and leaves a % without two hex digits after it as is)
- text&str
- 풀 글. (16 진 자리는 대소문자를 가리지 않는다)String to decode. (hex digits are case-insensitive)
반환ReturnsVec<u8> 푼 바이트. (UTF-8 이 아닐 수 있어 글이 아니라 바이트로 돌려준다)Decoded bytes. (returned as bytes rather than a string since it may not be UTF-8)
use vanilla::base::dataurl::decode_percent_encoding;
assert_eq!(decode_percent_encoding("a%20b%zz%4"), b"a b%zz%4".to_vec());