Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 2.68 KB | None | 0 0
  1.  
  2.         function isUTF8ExtensionChar(char) {
  3.             return (char >= "\200" && char < "\277")
  4.         }
  5.  
  6.         # Extract a substring from a UTF-8 encoded string. This is required
  7.         # since not all versions of awk respect the encoding specified by
  8.         # $LANG. Of particular interest for me is the default busybox awk
  9.         # within Alpine linux.
  10.         function substrUTF8(str, start, len, inLen, subLen, inIndex, subIndex, outLen) {
  11.             # Length of input string
  12.             inLen = length(str)
  13.  
  14.             # Current index into input string
  15.             inIndex = 1
  16.  
  17.             # Skip the initial unicode characters to get to starting index
  18.             while (subIndex < start && inIndex <= inLen) {
  19.                 if (!isUTF8ExtensionChar(substr(str, inIndex, 1))) {
  20.                     inIndex++
  21.                     subIndex++
  22.                     while (isUTF8ExtensionChar(substr(str, inIndex, 1))) {
  23.                         inIndex++
  24.                     }
  25.                 }
  26.             }
  27.  
  28.             # Length of substring of input string which will produce the
  29.             # output string
  30.             subLen = 0
  31.  
  32.             # Starting index of input string which corresponds to the first
  33.             # character of the output string
  34.             subIndex = 1
  35.  
  36.             # Number of true unicode characters counted for output string
  37.             outLen = 0
  38.  
  39.             # Calculate the end point of the sub string
  40.             while (outLen < len && inIndex <= inLen) {
  41.                 if (!isUTF8ExtensionChar(substr(str, inIndex, 1))) {
  42.                     inIndex++
  43.                     subLen++
  44.                     outLen++
  45.                     while (isUTF8ExtensionChar(substr(str, inIndex, 1))) {
  46.                         inIndex++
  47.                         subLen++
  48.                     }
  49.                 }
  50.             }
  51.  
  52.             return substr(str, subIndex, subLen);
  53.         }
  54.  
  55.         BEGIN {
  56.             path = ""
  57.             if (index(PWD, HOME)) {
  58.                 PWD = substr(PWD, length(HOME) + 1)
  59.                 path = "~"
  60.             }
  61.             split(PWD, dirs, "/")
  62.             for (i in dirs) {
  63.                 dir = dirs[i]
  64.                 if (dir != "") {
  65.                     if (i < length(dirs)) {
  66.                         if (dir ~ /^\./) {
  67.                             dir = substrUTF8(dir, 1, 2)
  68.                         } else {
  69.                             dir = substrUTF8(dir, 1, 1)
  70.                         }
  71.                     }
  72.                     path = path "/" dir
  73.                 }
  74.             }
  75.             if (path == "") {
  76.                 path = "/"
  77.             }
  78.             print path
  79.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement