question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

[Android] [RN 58.3] Calling substring() on a String that contains an emoji causes "Error: Exception in HostFunction: basic_string::resize"

See original GitHub issue

๐Ÿ› Bug Report

On Android (I couldnโ€™t test on iOS), with react-native 0.58.3, try to call substring(0,1) on a string that begins with an emoji. You should get this error: [Error: Exception in HostFunction: basic_string::resize] I guess this is linked to some bad conversion from UTF 16 to UTF 8.

This is not happening on 0.57.3 for instance (which I was using earlier for the same project)

Code Example

const text = '๐Ÿ™ƒblablabla'
try {
    const newText = text.substring(0, 1)
    Alert.alert(newText)
  } catch(e) {
    console.error(e)
}

Environment

React Native Environment Info:
System:
  OS: macOS High Sierra 10.13.5
  CPU: (4) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
  Memory: 41.97 MB / 8.00 GB
  Shell: 3.2.57 - /bin/bash
Binaries:
  Node: 10.15.0 - ~/.nvm/versions/node/v10.15.0/bin/node
  Yarn: 1.12.3 - /usr/local/bin/yarn
  npm: 6.4.1 - ~/.nvm/versions/node/v10.15.0/bin/npm
  Watchman: 4.7.0 - /usr/local/bin/watchman
SDKs:
  iOS SDK:
    Platforms: iOS 10.3, macOS 10.12, tvOS 10.2, watchOS 3.2
  Android SDK:
    API Levels: 23, 24, 25, 26, 27, 28
    Build Tools: 23.0.1, 23.0.2, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.1, 27.0.3, 28.0.0, 28.0.3
    System Images: android-27 | Google APIs Intel x86 Atom, android-28 | Google APIs Intel x86 Atom_64
IDEs:
  Android Studio: 3.1 AI-173.4670197
  Xcode: 8.3.3/8E3004b - /usr/bin/xcodebuild
npmPackages:
  react: 16.6.3 => 16.6.3 
  react-native: 0.58.3 => 0.58.3 
npmGlobalPackages:
  react-native-git-upgrade: 0.2.7

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
levibuzoliccommented, Feb 18, 2019

@OlivierFreyssinet this is a limitation of multi-byte characters in JS strings. While ideally it shouldnโ€™t crash/error so badly in React Native it is something that should be accounted for in user-land JS.

'๐Ÿ™ƒ'.length
// 2

As you can see this is a multi-byte character so youโ€™ll need to use multi-byte compatible operations if you want to handle the emoji correctly.

As @wakeless pointed out using Array.from() should be safer in most cases however it will give surprising results on emojis that use joining characters like ๐Ÿ‘ฉโ€โค๏ธโ€๐Ÿ’‹โ€๐Ÿ‘ฉ:

'๐Ÿ‘ฉโ€โค๏ธโ€๐Ÿ’‹โ€๐Ÿ‘ฉ'.length
// 11

Array.from('๐Ÿ‘ฉโ€โค๏ธโ€๐Ÿ’‹โ€๐Ÿ‘ฉ')
// ["๐Ÿ‘ฉ", "", "โค", "", "", "๐Ÿ’‹", "", "๐Ÿ‘ฉ"]

If you want to handle these cases, a library like https://www.npmjs.com/package/runes might useful.

import runes from "runes";

runes('๐Ÿ‘ฉโ€โค๏ธโ€๐Ÿ’‹โ€๐Ÿ‘ฉ')
// ["๐Ÿ‘ฉโ€โค๏ธโ€๐Ÿ’‹โ€๐Ÿ‘ฉ"]
5reactions
ericlewiscommented, Feb 23, 2019

I can confirm that this issue occurs on iOS as well, it appears something else has changed. I am looking in to it.

Error: Exception in HostFunction: basic_string
Read more comments on GitHub >

github_iconTop Results From Across the Web

Check if there is an emoji contained in a string
The only problem I have is that if the string only contains an emoji, my app crashes. Is there an easy way to...
Read more >
EmojiCompat | Android Developers
Main class to keep Android devices up to date with the newest emojis by adding ... CharSequence processedSequence = EmojiCompat.get().process("some string")ย ...
Read more >
It's not wrong that "๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ".length == 7
The string contains a single emoji consisting of five Unicode scalar ... The len() method call just returns this number that has been...
Read more >
Dart string manipulation done right ๐Ÿ‘‰ | by Tao Dong
In the string โ€œHello๐Ÿ‘‹โ€, each user-perceivable character is mapped to a single code unit except the waving hand emoji ๐Ÿ‘‹.
Read more >
Remove Emojis from a Java String
Have a look at ways to remove emojis from a text in Java.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found