什么是符文? [英] What is a rune?

查看:139
本文介绍了什么是符文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是Go中的符文



我一直在使用Google,但Golang只在一行中说: rune int32 的别名。

但是如何使用整数像交换事件一样?



以下是一个函数swapcase。
什么是< = -



为什么 switch 没有任何参数?

&& 应该是,但 r <='z'?

  func SwapRune(r rune)rune {
switch {
case'a'< = r&& r< ='z':
return r - 'a'+'A'
'A'< = r&& r <='Z':
return r - 'A'+'a'
default:
return r
}
}

其中大部分来自 http: //play.golang.org/p/H6wjLZj6lW

  func SwapCase(str string)string {
返回strings.Map(SwapRune,str)
}

我明白这是mapping rune string ,以便它可以返回交换的字符串。但我不明白 rune byte 在这里是如何工作的。

解决方案

符文文字只是一个整数值(如您所写)。他们被映射到他们的unicode代码点。例如,规则字面值'a'实际上是97.



因此,您的程序几乎等同于:

  package main 

importfmt

func SwapRune(r rune)rune {
switch {
情况97< = r& amp;& r <= 122:
return r - 32
case 65< = r&& r <= 90:
return r + 32
default:
return r
}
}

func main(){
fmt.Println(SwapRune('a'))
}

如果你看一下unicode映射,这个映射与那个范围内的 ASCII 是一样的。而32是大写和小写字符的偏移量。所以如果你把32加到'A',你会得到'a',反之亦然。


What is a rune in Go?

I've been googling but Golang only says in one line: rune is an alias for int32.

But how come integers are used all around like swapping cases?

The following is a function swapcase. What is all the <= and -?

And why doesn't switch have any arguments?

&& should mean and but what is r <= 'z'?

func SwapRune(r rune) rune {
    switch {
    case 'a' <= r && r <= 'z':
        return r - 'a' + 'A'
    case 'A' <= r && r <= 'Z':
        return r - 'A' + 'a'
    default:
        return r
    }
}

Most of them are from http://play.golang.org/p/H6wjLZj6lW

func SwapCase(str string) string {
    return strings.Map(SwapRune, str)
}

I understand this is mapping rune to string so that it can return the swapped string. But I do not understand how exactly rune or byte works here.

解决方案

Rune literals are just an integer value (as you've written). They are "mapped" to their unicode codepoint. For example the rule literal 'a' is in reality the number 97.

Therfore your program is pretty much equivalent to:

package main

import "fmt"

func SwapRune(r rune) rune {
    switch {
    case 97 <= r && r <= 122:
        return r - 32
    case 65 <= r && r <= 90:
        return r + 32
    default:
        return r
    }
}

func main() {
    fmt.Println(SwapRune('a'))
}

which should be obvious if you look at the unicode mapping, which is identical to ASCII in that range. And 32 is the offset of the uppercase and lowercase characters. So if you add 32 to 'A', you get 'a' and vice versa.

这篇关于什么是符文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆