utf16string - Rust - Docs.rs

文章推薦指數: 80 %
投票人數:10人

However different CPU architectures encode these u16 integers using different byte order: little-endian and big-endian. Thus when handling UTF-16 strings you ... Docs.rs utf16string-0.2.0 utf16string0.2.0 Permalink Docs.rscratepage MITORApache-2.0 Links Repository Crates.io Source Owners flub Dependencies byteorder^1 normal Versions 0.2.0 0.1.0 100.0% ofthecrateisdocumented Platform x86_64-unknown-linux-gnu i686-unknown-linux-gnu x86_64-pc-windows-msvc x86_64-apple-darwin i686-pc-windows-msvc Featureflags Releases AllReleases ReleasesbyStars RecentBuildFailures BuildFailuresbyStars ReleaseActivity Rust Rustwebsite TheBook StandardLibraryAPIReference RustbyExample TheCargoGuide ClippyDocumentation Aboutdocs.rs Privacypolicy Buildqueue ☰Crateutf16stringVersion0.2.0Seeallutf16string'sitemsStructsEnumsTraitsTypeDefinitions? [−][src]Crateutf16stringAUTF-16little-endianstringtype. ThiscrateprovidestwostringtypestohandleUTF-16encodedbytesdirectlyasstrings: WStringandWStr.TheyaretoUTF-16exactlylikeStringandstrareto UTF-8.Someoftheconceptsandfunctionshereareratherterselydocumented,inthis caseyoucanlookuptheirequivalentsonStringorstrandthebehaviourshould beexactlythesame,onlytheunderlyingbyteencodingisdifferent. ThusWStringisatypewhichownsthebytescontainingthestring.Justlike StringandtheunderlyingVecitisbuilton,itdistinguisheslength (WString::len)andcapacity(String::capacity).Herelengthisthenumberof bytesusedwhilecapacityisthenumberofbytesthestringcangrowwithouth reallocating. TheWStrtypedoesnotownanybytes,itcanonlypointtoasliceofbytes containingvalidUTF-16.Assuchyouwillonlyeveruseitasareferencelike&WStr, justyouyouonlyusestras&str. TheWStringtypeimplementsDeref UTF-16ByteOrder UTF-16encodestounsigned16-bitintegers(u16),denotingcodeunits.However differentCPUarchitecturesencodetheseu16integersusingdifferentbyteorder: little-endianandbig-endian.ThuswhenhandlingUTF-16stringsyouneedtobe awareofthebyteorderoftheencoding,commonlytheencodingvariantsareknowas UTF-16LEandUTF-16BErespectively. Forthiscratethismeansthetypesneedtobeawareofthebyteorder,whichisdone usingthebyteorder::ByteOrdertraitasagenericparametertothetypes: WStringandWStrcommonlywrittenasWStringand WStrwhereEstandsfor"endianess". ThiscrateexportsBigEndian,BE,LittleEndianandLEincaseyouneed todenotethetype: useutf16string::{BigEndian,BE,WString}; lets0:WString=WString::from("hello"); assert_eq!(s0.len(),10); lets1:WString=WString::from("hello"); assert_eq!(s0,s1); Asthesetypescanoftenbeabitcumbersometowritetheycanoftenbeinferred, especiallywiththehelpoftheshorthandconstructorslikeWString::from_utf16le, WString::from_utf16be,WStr::from_utf16le,WStr::from_utf16beandrelated. Forexample: useutf16string::{LE,WStr}; letb=b"h\x00e\x00l\x00l\x00o\x00"; lets0:&WStr=WStr::from_utf16(b)?; lets1=WStr::from_utf16le(b)?; assert_eq!(s0,s1); assert_eq!(s0.to_utf8(),"hello"); Structs Utf16ErrorErrorforinvalidUTF-16encodedbytes. WStrAUTF-16str-liketypewithlittle-orbig-endianbyteorder. WStrCharIndicesIteratoryielding(index,char)tuplesfromaUTF-16little-endianencodedbyteslice. WStrCharsIteratoryieldingcharfromaUTF-16encodedbyteslice. WStringAUTF-16String-liketypewithlittle-orbig-endianbyteorder. Enums BigEndianLittleEndianTraits SliceIndexOurownversionofstd::slice::SliceIndex. TypeDefinitions BELE



請為這篇文章評分?