Search This Blog

Showing posts with label develop. Show all posts
Showing posts with label develop. Show all posts

Java: check if a String contains only ASCII

import java.nio.charset.Charset;

public boolean isAscii(String v) {
    Charset.forName("US-ASCII").newEncoder().canEncode(v);
}

Eclipse: Disable auto escaping when pasting

 

  1. In the preferences menu deselect: Java > Editor > Typing > Escape text when pasting into a string literal
  2. Disable smart insert mode: Edit > Smart Insert Mode [Ctrl+Shift+Insert]

Java: remove unsafe html tags


public class HtmlTagRemover {
    public static String removeUnsafeTags(String html) {
        // Define the regular expression pattern to match unsafe tags
        String unsafeTagsPattern = "<(script|iframe|object|embed)[^>]*>.*?</\\1>";

        // Remove the unsafe tags from the HTML string
        String safeHtml = html.replaceAll(unsafeTagsPattern, "");

        return safeHtml;
    }

    public static void main(String[] args) {
        String html = "<b>Safe HTML</b><script>alert('XSS')</script>";
        String safeHtml = removeUnsafeTags(html);
        System.out.println(safeHtml);
    }
}

Regular Expression Capture Groups

  • Capture groups:
    
    use regex::Regex;
    
    let re = Regex::new(r"(\w+)").unwrap();
    let text = "Hello world";
    
    for cap in re.captures_iter(text) {
        println!("Match: {}", &cap[1]);
    }
    
    
  • Named capture groups:
    
    use regex::Regex;
    
    let re = Regex::new(r"(?<word>\w+)").unwrap();
    let text = "Hello world";
    
    for cap in re.captures_iter(text) {
        println!("Match: {}", &cap["word"]);
    }
    
    

Enable code syntax highlighting in blogger site using highlightjs

  • Template -> Edit HTML
  • Insert the following inside <head> tag:
        
        <!-- BEGIN: Syntax Highlighting with highlight.js -->
        <link crossorigin='anonymous' href='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/agate.min.css' integrity='sha512-wI7oXtzNHj/bqfLA3P6x3XYbcwzsnIKaPLfjjX8ZAXhc65+kSI6sh8gLOOByOKImokAjHUQR0xAJQ/xZTzwuOA==' referrerpolicy='no-referrer' rel='stylesheet'/>
        <script crossorigin='anonymous' integrity='sha512-bgHRAiTjGrzHzLyKOnpFvaEpGzJet3z4tZnXGjpsCcqOnAH6VGUx9frc5bcIhKTVLEiCO6vEhNAgx5jtLUYrfA==' referrerpolicy='no-referrer' src='https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js'/>
        <script>hljs.highlightAll();</script>
        <!-- END: Syntax Highlighting with highlight.js -->
        <!-- BEGIN: CSS for code in pre and code -->
        <style type='text/css'>
            pre {
                font-family: monospace;
                font-size: 1 em;
                background: #333;
                color: #33cc33;
                tab-size: 4;
                padding: 5px;
                border: 1px dotted grey;
                border-radius: 5px;
                line-height: 2.0;
                overflow-x: auto;
                white-space: pre-wrap;
            }
    
            code {
                white-space: pre-wrap;
                overflow-x: scroll;    
                font-family: monospace;
                font-size: 1em;
            }
        </style>
        <!-- END: CSS for code in pre and code -->
        
        
  • Click save icon to save the template
  • Use it in your blog post:
    <pre><code class="language-html">...</code></pre>



See also

Setup Neovim for Rust Development

  1. Install Neovim
  2. Install AstroNvim
    • After installed AstroNvim, you can customise it
  3. Install rust LSP
    In Neovim, execute command:
    :LspInstall rust
  4. Install rust language parser (tree-sitter)
    In Neovim, execute command:
    :TSInstall rust
  5. Install lua language parser (tree-sitter)
    In Neovim, execute command:
    :TSInstall lua
    This is useful for editting the Neovim config (lua) files (*.lua)





See also

Migrate git repository to GitLab using the command line

  • Create a new EMPTY repository in GitLab. (Note: You MUST uncheck "Initialize repository with README")
  • Clone from the source remote repository (from e.g. BitBucket, Github):
    git clone --mirror git@bitbucket.org:path/to/REPO.git
  • Push the local repository to GitLab:
    cd REPO.git
    git push --mirror git@gitlab.com:path/to/REPO.git



see also

Set Groovy Development Environment

I. Install required SDK and IDE

  1. Install Java SDK 8+
  2. Install Gradle
  3. Install Groovy
  4. Install Intellij Idea



II. Create a new Groovy project using Gradle

  1. Create a new project directory:
    mkdir my-groovy-app
  2. Get into the project directory
    cd my-groovy-app
    and run gradle:
    gradle init
    Select type of project to generate:
      1: basic
      2: application
      3: library
      4: Gradle plugin
    Enter selection (default: basic) [1..4] 1
    
    Select build script DSL:
      1: Groovy
      2: Kotlin
    Enter selection (default: Groovy) [1..2] 1
    
    Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]
    Project name (default: my-groovy-app):
    
    > Task :init
    Get more help with your project: Learn more about Gradle by exploring our samples at https://docs.gradle.org/7.6/samples
    
    BUILD SUCCESSFUL in 22s
    2 actionable tasks: 2 executed
        
  3. Edit build.gradle file and add the content (like below):
    plugins {
        id 'groovy'
        id 'application'
    }
    
    group 'myapps'
    version '1.0-SNAPSHOT'
    
    repositories {
        mavenCentral()
        maven {
            url 'https://my.org/mvn-repo'
        }
    }
    
    dependencies {
        implementation 'org.apache.groovy:groovy:4.0.7'
        implementation 'org.apache.ivy:ivy:2.5.0'
    
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    }
    
    application {
    	mainClass = 'my.App'
    }
    
    test {
        useJUnitPlatform()
    }
        
  4. Create source folder structure:
    mkdir src/main/groovy
    mkdir src/main/java
    mkdir src/main/resources
    mkdir src/test/groovy
    mkdir src/test/java
    mkdir src/test/resources
        
  5. Optional: create gradle.properties file at the project root, and add the options below:
    org.gradle.warning.mode=all
    org.gradle.logging.stacktrace=full
  6. Create Application class: my.App.groovy:
    package my
    
    class App {
        static void main(String[] args) {
        	println 'Hello, Wolrd'
        }
    }
      
  7. Start Intellij Idea, open the project directory. Select src/main/groovy directory, Mark directory as -> Sources root

Typescript optional function


function test(msg: string, func?: (m: string)=>void) {
	func?.(a, b);
}

test('nothing');

test('something', (m)=>console.log(m));

Typescript array elements map to objects

interface Person { name: string; email?: string }

array.map(val => <Person> {
  name:  val.key1,
  email: val.key2
});

Python code to detect file name encoding

File name encoding on Linux

Linux File Name Encoding

iocharset option of mount

iocharset=value
Character set to use for converting between 8 bit characters and 16 bit Unicode characters. 
The default is iso8859-1. Long filenames are stored on disk in Unicode format.

convmv - converts filenames from one encoding to another

See also

Javascript: sort string array alphabetically

localeCompare() method can be used:
const array = ['b', 'd', 'c', 'a'];
array.sort((a,b)=>a.localeCompare(b));



see also

Javascript: remove duplicated values from array

Get distinct values from Array
let array = ['aaa', 'bbb', 'ccc', 'bbb', 'ddd'];
console.log(array);

array = Array.from(new Set(array));
console.log(array);



Get distinct values from Map
const map = new Map<string, number>();
map.set('foo', 1);
map.set('bar', 2);
map.set'zoo', 1);
const distinctValues = Array.from(new Set(map.values()));



see also