Socket
Socket
Sign inDemoInstall

decancer

Package Overview
Dependencies
12
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    decancer

A tiny package that removes common unicode confusables/homoglyphs from strings.


Version published
Weekly downloads
17
decreased by-19.05%
Maintainers
1
Created
Weekly downloads
Β 

Readme

Source

decancer npm crates.io npm downloads crates.io downloads code style: prettier Build Status license BLAZINGLY FAST!!!

A tiny package that removes common unicode confusables/homoglyphs from strings.

  • Its core is written in Rust and utilizes a form of Binary Search to ensure speed! Absolutely NO regular expressions are used.
  • By default, it's capable of filtering 215.356 (19.33%) different unicode codepoints like:
  • Unlike other packages, this package is unicode bidi-aware in a way that it also interprets right-to-left characters in the same way as it were to be rendered by an application.
  • Its behavior is also highly customizable to your liking!
  • And it's available in the following languages:

Installation

Rust (v1.64 or later)

In your Cargo.toml:

decancer = "3.0.1"
JavaScript (Node.js)

In your shell:

$ npm install decancer

In your code (CommonJS):

const decancer = require('decancer')

In your code (ESM):

import decancer from 'decancer'
JavaScript (Browser)

In your code:

<script type="module">
  import init from 'https://cdn.jsdelivr.net/gh/null8626/decancer@v3.0.1/bindings/wasm/bin/decancer.min.js'

  const decancer = await init()
</script>
Java

As a dependency

In your build.gradle:

repositories {
  mavenCentral()
  maven { url 'https://jitpack.io' }
}

dependencies {
  implementation 'com.github.null8626:decancer:v3.0.1'
}

In your pom.xml:

<repositories>
  <repository>
    <id>central</id>
    <url>https://repo.maven.apache.org/maven2</url>
  </repository>
  <repository>
    <id>jitpack.io</id>
    <url>https://jitpack.io</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>com.github.null8626</groupId>
    <artifactId>decancer</artifactId>
    <version>v3.0.1</version>
  </dependency>
</dependencies>

Building from source

$ git clone https://github.com/null8626/decancer.git --depth 1
$ cd ./decancer/bindings/java
$ unzip ./bin/bindings.zip -d ./bin
$ chmod +x ./gradlew
$ ./gradlew build --warning-mode all
C/C++

Download

Building from source

Building from source requires Rust v1.64 or later.

$ git clone https://github.com/null8626/decancer.git --depth 1
$ cd decancer/bindings/native
$ cargo build --release

And the binary files should be generated in the target/release directory.

Examples

Rust

For more information, please read the documentation.

let cured = decancer::cure!("vοΌ₯ⓑ𝔂 π”½π•ŒΕ‡β„•ο½™ ţ乇𝕏𝓣").unwrap();

assert_eq!(cured, "very funny text");
assert!(cured.contains("FuNny"));
assert_eq!(cured.into_str(), String::from("very funny text"));
JavaScript (Node.js)
const assert = require('assert')
const cured = decancer('vοΌ₯ⓑ𝔂 π”½π•ŒΕ‡β„•ο½™ ţ乇𝕏𝓣')

assert(cured.equals('very funny text'))
assert(cured.contains('funny'))

console.log(cured.toString())
// => 'very funny text'
JavaScript (Browser)
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Decancerer!!! (tm)</title>
    <style>
      textarea {
        font-size: 30px;
      }

      #cure {
        font-size: 20px;
        padding: 5px 30px;
      }
    </style>
  </head>
  <body>
    <h3>Input cancerous text here:</h3>
    <textarea rows="10" cols="30"></textarea>
    <br />
    <button id="cure" onclick="cure()">cure!</button>
    <script type="module">
      import init from 'https://cdn.jsdelivr.net/gh/null8626/decancer@v3.0.1/bindings/wasm/bin/decancer.min.js'

      const decancer = await init()

      window.cure = function () {
        const textarea = document.querySelector('textarea')

        if (!textarea.value.length) {
          return alert("There's no text!!!")
        }

        textarea.value = decancer(textarea.value).toString()
      }
    </script>
  </body>
</html>

See this in action here.

Java
import com.github.null8626.decancer.CuredString;

public class Program {
  public static void main(String[] args) {
    final CuredString cured = new CuredString("vοΌ₯ⓑ𝔂 π”½π•ŒΕ‡β„•ο½™ ţ乇𝕏𝓣");
    
    assert cured.equals("very funny text");
    assert cured.contains("funny");
    
    System.out.println(cured.toString());
    
    cured.destroy();
  }
}
C/C++
#include <decancer.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

// global variable for assertion purposes only
decancer_cured_t cured;

static void assert(const bool expr, const char *message)
{
    if (!expr)
    {
        fprintf(stderr, "assertion failed (%s)\n", message);
        decancer_free(cured);
        
        exit(1);
    }
}

static void print_error(decancer_error_t error_code)
{
    char message[90];
    uint8_t message_size;
    
    const uint8_t *ptr = decancer_error(error_code, &message_size);
    memcpy(message, ptr, message_size);
   
    // rust strings are NOT null-terminated
    message[message_size] = '\0';
    
    fprintf(stderr, "error: %s", message);
}

int main(void) {
    decancer_error_t error_code;

    // utf-8 bytes for "vοΌ₯ⓑ𝔂 π”½π•ŒΕ‡β„•ο½™ ţ乇𝕏𝓣"
    uint8_t string[] = {0x76, 0xef, 0xbc, 0xa5, 0xe2, 0x93, 0xa1, 0xf0, 0x9d, 0x94, 0x82, 0x20, 0xf0, 0x9d,
                        0x94, 0xbd, 0xf0, 0x9d, 0x95, 0x8c, 0xc5, 0x87, 0xe2, 0x84, 0x95, 0xef, 0xbd, 0x99,
                        0x20, 0xc5, 0xa3, 0xe4, 0xb9, 0x87, 0xf0, 0x9d, 0x95, 0x8f, 0xf0, 0x9d, 0x93, 0xa3};

    cured = decancer_cure(string, sizeof(string), DECANCER_OPTION_DEFAULT, &error_code);

    if (cured == NULL)
    {
        print_error(error_code);
        return 1;
    }

    assert(decancer_equals(cured, (uint8_t *)("very funny text"), 15), "equals");
    assert(decancer_contains(cured, (uint8_t *)("funny"), 5), "contains");

    // coerce output as a raw UTF-8 pointer and retrieve its size (in bytes)
    size_t output_size;
    const uint8_t *output_raw = decancer_raw(cured, &output_size);

    assert(output_size == 15, "raw output size");

    // utf-8 bytes for "very funny text"
    const uint8_t expected_raw[] = {0x76, 0x65, 0x72, 0x79, 0x20, 0x66, 0x75, 0x6e,
                                    0x6e, 0x79, 0x20, 0x74, 0x65, 0x78, 0x74};

    char assert_message[38];
    for (uint32_t i = 0; i < sizeof(expected_raw); i++)
    {
        sprintf(assert_message, "mismatched utf-8 contents at index %u", i);
        assert(output_raw[i] == expected_raw[i], assert_message);
    }

    decancer_free(cured);    
    return 0;
}

Compatibility

Decancer is supported in the following platforms:

Platform nameC/C++/RustJavaJavaScript
ARM64 macOS (11.0+, Big Sur+)βœ“βœ“βœ“
ARM64 iOSβœ“
Apple iOS Simulator on ARM6βœ“
ARM64 Androidβœ“βœ“
ARM64 Windows MSVCβœ“βœ“βœ“
ARM64 Linux (kernel 4.1, glibc 2.17+)βœ“βœ“βœ“
ARM64 Linux with MUSLβœ“βœ“βœ“
ARMv6 Linux (kernel 3.2, glibc 2.17)βœ“βœ“
ARMv5TE Linux (kernel 4.4, glibc 2.23)βœ“βœ“
ARMv7-A Androidβœ“βœ“
ARMv7-A Linux (kernel 4.15, glibc 2.27)βœ“βœ“
ARMv7-A Linux, hardfloat (kernel 3.2, glibc 2.17)βœ“βœ“βœ“
32-bit Linux w/o SSE (kernel 3.2, glibc 2.17)βœ“
32-bit MSVC (Windows 7+)βœ“βœ“βœ“
32-bit FreeBSDβœ“βœ“
32-bit Linux (kernel 3.2+, glibc 2.17+)βœ“βœ“
PPC64LE Linux (kernel 3.10, glibc 2.17)βœ“
RISC-V Linux (kernel 4.20, glibc 2.29)βœ“βœ“
S390x Linux (kernel 3.2, glibc 2.17)βœ“
SPARC Solaris 11, illumosβœ“
Thumb2-mode ARMv7-A Linux with NEON (kernel 4.4, glibc 2.23)βœ“
64-bit macOS (10.12+, Sierra+)βœ“βœ“βœ“
64-bit iOSβœ“
64-bit MSVC (Windows 7+)βœ“βœ“βœ“
64-bit FreeBSDβœ“βœ“
64-bit illumosβœ“
64-bit Linux (kernel 3.2+, glibc 2.17+)βœ“βœ“βœ“
64-bit Linux with MUSLβœ“βœ“βœ“

Contributing

Please read CONTRIBUTING.md for newbie contributors who want to contribute!

Keywords

FAQs

Last updated on 27 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc