theweipeng commented on code in PR #1730:
URL: https://github.com/apache/fury/pull/1730#discussion_r1681231212


##########
rust/fury/src/util.rs:
##########
@@ -0,0 +1,122 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Swapping the high 8 bits and the low 8 bits of a 16-bit value
+fn swap_endian(value: u16) -> u16 {
+    (value << 8) | (value >> 8)
+}
+
+pub fn to_utf8(utf16: &[u16], is_little_endian: bool) -> Result<Vec<u8>, 
String> {
+    // Pre-allocating capacity to avoid dynamic resizing
+    // Longest case: 1 u16 to 3 u8
+    let mut utf8_bytes: Vec<u8> = Vec::with_capacity(utf16.len() * 3);
+    // For unsafe write to Vec
+    let ptr = utf8_bytes.as_mut_ptr();
+    let mut offset = 0;
+    let mut iter = utf16.iter();
+    while let Some(&wc) = iter.next() {
+        // Using big endian in this conversion
+        let wc = if is_little_endian {
+            swap_endian(wc)
+        } else {
+            wc
+        };
+        match wc {
+            code_point if code_point < 0x80 => {
+                // 1-byte UTF-8
+                // [0000|0000|0ccc|cccc] => [0ccc|cccc]
+                unsafe {
+                    ptr.add(offset).write(code_point as u8);
+                }
+                offset += 1;
+            }
+            code_point if code_point < 0x800 => {
+                // 2-byte UTF-8
+                // [0000|0bbb|bbcc|cccc] => [110|bbbbb], [10|cccccc]
+                // write a u16 to [u8;2] is affected by the processor's 
endianness.
+                #[cfg(target_endian = "little")]

Review Comment:
   We can use copy_from_slice 
   ```
   let sl = utf8_bytes.as_mut_slice()
   sl.copy_from_slice(&[u1,u2,u3,u4]);
   ```
   Or copy_nonoverlapping
   ```
   ptr::copy_nonoverlapping(src.as_ptr(), self.as_mut_ptr(), self.len());
   ```
   
   ```
   to avoid the multiple times memory write overhead.  Meanwhile, enhance the 
readability of the code
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to