xaizek / hstr (License: Apachev2) (since 2018-12-07)
Bash and Zsh shell history suggest box - easily view, navigate, search and manage your command history.
<root> / tests / src / test_utf8.c (b619a1c760d9493e50ef7b8ba70b038d6b416ca3) (4,858B) (mode 100644) [raw]
/*
 ============================================================================
 Name        : test_utf8.c
 Author      : martin.dvorak@mindforger.com
 Copyright   : Apache 2.0
 Description : A test
 ============================================================================
*/

#include <locale.h>
#include <wchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>
#include <readline/readline.h>
#include <readline/chardefs.h>

#define BITS_IN_BYTE 8
#define INTEGRAL_TYPE char
void show_bits(INTEGRAL_TYPE x) {
    int i;
    static int intSizeInBits = sizeof(INTEGRAL_TYPE) * BITS_IN_BYTE;
    static char symbol[2] = {'0','1'};
    char * binary = (char *)malloc(intSizeInBits + 1);

    memset(binary, 0, intSizeInBits + 1);

    for (i=0; i< intSizeInBits; i++) {
        binary[intSizeInBits-i-1] = symbol[(x>>i) & 0x01];
    }

    printf("\n%s", binary);
    printf("\n1234567.1234567.1234567.1234567.");
    free(binary);
}

void console_echo_czech()
{    
    int c;
    while(1) {
      c = getc(stdin);
      printf("\nKey: '%3d', char: '%c'", c, c);
      show_bits(c);
    }
}

void loop_string() {
//      char *s="a";
      char *s="Ča";
//      char *s="Čeština";
      int i;
      for(i=0; i<10; i++) {
          printf("\n%d",s[i]);
          show_bits(s[i]);
          if(!s[i]) break;
      }
}

void get_string_length() {
      char *s="Čeština";
      wchar_t *w=L"Čeština";

      printf("%s (7): strlen(): %zd, mbstowcs(): %zd, wcslen(): %zd",
              s,
              strlen(s),
              mbstowcs(NULL,s,0),
              wcslen(w));         // OK
}

void console_static_wide_czech()
{
  setlocale(LC_ALL, "");

  wchar_t *w=L"Čeština."; // wide
  char multibyte[100]; // multi-byte
  if(iswprint(*w)) {
      printf("\nString to be printed is UTF8 wide!");
      int offset=wctomb(multibyte, w);
      printf("\nStatic (wide) printf: %s", multibyte);
  } else {
      printf("\nString to be printed is NOT UTF8 wide!");
      wprintf(L"\nStatic wprintf: %ls", w);
  }
}

void console_static_czech()
{
  setlocale(LC_ALL, "");

  char *s="Čeština ěščřžýáíé.";  
  printf("\nStatic printf: %s", s);
}

void console_check() 
{
  printf("\nEnglish string.");
}

void curses_wide_czech()
{
  char *s="Čeština ěščřžýáíé.";  
  printf("Going to print the following string in Curses: '%s'", s);
  getch();

  WINDOW *stdscr;
  setlocale(LC_ALL, "");

  stdscr=initscr();
  mvwprintw(stdscr,0,0,s);
  getch();
  endwin();
}

void print_char_bits(int y, int x, char c) {
    int i;
    static int intSizeInBits = sizeof(char) * 8;
    static char symbol[2] = {'0','1'};
    char * binary = (char *)malloc(intSizeInBits + 1);
    memset(binary, 0, intSizeInBits + 1);
    for (i=0; i< intSizeInBits; i++) {
        binary[intSizeInBits-i-1] = symbol[(c>>i) & 0x01];
    }
    mvprintw(y, x, "%s", binary);
    free(binary);

}

void getch_with_counter_curses() {
    // TODO implement getch with counter; getch result analysis (trip); append analysis; ...

    initscr();
    keypad(stdscr, TRUE);
    noecho();
    start_color();
    use_default_colors();

    char pattern[512];
    int c;

    pattern[0]=0;
    while (1) {
        c = wgetch(stdscr);
        strcat(pattern, (char*)(&c));
        mvprintw(2, 0, "Pattern '%s'", pattern);
        mvprintw(3, 0, "Char    '%d'", c);

        mvprintw(6, 0, "strlen()   '%d'", strlen(pattern));
        mvprintw(7, 0, "mbstowcs() '%d'", mbstowcs(NULL,pattern,0));

        int i;
        int intSizeInBits = sizeof(int) * 8;
        char symbol[2] = {'0','1'};
        char * binary = (char *)malloc(intSizeInBits + 1);
        memset(binary, 0, intSizeInBits + 1);
        for (i=0; i< intSizeInBits; i++) {
            binary[intSizeInBits-i-1] = symbol[(c>>i) & 0x01];
        }
        mvprintw(10, 0, "bits:     %s", binary);
        free(binary);

        mvprintw(11, 0, "high bit: %d %d      ", 1<<7, 1<<7 & c);

        char cc=pattern[0];
        i=0;
        int myStrlen=0;
        char isHighBitSet=0;
        while(cc) {
            print_char_bits(12, 9*i-8, pattern[i++]);
            cc=pattern[i];

            if(1<<7 & pattern[i]) {
                if(isHighBitSet) {
                    isHighBitSet=0;
                    myStrlen++;
                } else {
                    isHighBitSet=1;
                }
            } else {
                myStrlen++;
            }
        }

        mvprintw(14, 0, "mystrlen(): %d   ", myStrlen);
    }

    clear();
    refresh();
    doupdate();
    endwin();
}

void done() {
      printf("\n\n");
}

int main(int argc, char *argv[])
{
  //console_check();
  //console_static_czech();
  //console_static_wide_czech();
  //console_echo_czech();
  //curses_wide_czech();
  //get_string_length();
  //loop_string();

    getch_with_counter_curses();
    done();
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/hstr

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/hstr

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master