const{createApp}=Vue const{createVuetify,useGoTo,useDisplay}=Vuetify var data={alert:{show:false,color:'success',text:'',timeout:0,},theme:{dark:false,},nav:{showDrawer:false,showTOC:true,tocPanel:0,tab:'account',post:{discussionId:430,currentPage:1,targetPage:1,total:33,anchor:0,goToOptions:{container:null,duration:0,easing:'easeInOutCubic',offset:-100,},worker:null,task:[],active:[],apiLock:[],originLike:new Map([]),},related:{block:1,}},search:{width:80,text:null,loading:false,},tags:[{id:26,url:'/t/317a6a296a045c7566775e0c475c',name:'C|CPP',color:'#FFA726',icon:'mdi-language-cpp',},],posts:[{id:5490,num:0,uid:2611,content:'如题,今天给 C 库写其他语言 binding 的时候想到的。\u003Cbr\u003E\u003Cbr\u003E论据 1:C/C++ 之后,几乎所有语言都 (1) 定死了各个整数类型的宽度(如 J**a ),或者 (2) 换用定宽类型(例如 i64 、u32 、int64 、usize )。即使 C 自己也引入了 int64_t 这样的固定宽度类型。\u003Cbr\u003E\u003Cbr\u003E论据 2:非常不利于编写可移植的库。比如用两个不同编译器编译出的代码,虽然函数声明一样,但因为一边 long 是 64 位整数,另一边是 32 位整数,导致不能互相调用。\u003Cbr\u003E\u003Cbr\u003E论据 3:不确定宽度导致程序员不得不随时检查数值范围,一旦疏忽(例如,换了新平台或者编译器)很容易造成溢出问题。为了检查,编写大量含宏代码,给程序员带来额外心智负担。\u003Cbr\u003E\u003Cbr\u003E论据 4:使数值类型系统变得过于复杂,很多场景下并不需要严格区分 long long 、long 、int 、short 、char 。但程序员由于认识不清楚而随意使用,反而可能造成 linter 不能正确给出 warning 。\u003Cbr\u003E\u003Cbr\u003E---\u003Cbr\u003E\u003Cbr\u003E这个 int 、long 不定宽的规定是当时随意决定的吗?还是刻意设计出来的?为什么直到今天还有大量 C 库、接口仍在使用这样的类型?(例如 POSIX 、libc 、OpenMP )',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 22:33:22',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5491,num:1,uid:2612,content:'刻意设计出来的,为了匹配当时的处理器的一个 world ,参考: https://www.quora.com/Why-are-type-sizes-inconsistent-among-different-platforms-eg-short-int-long-in-C',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 22:44:43',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5492,num:2,uid:2613,content:'如果让我从 c99 前的整数类型和 j**a 的整数类型二选一,我还是选宁愿用 c 的',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 22:44:46',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5493,num:3,uid:2614,content:'当然是失败设计。\u003Cbr\u003E\u003Cbr\u003Erust 的更好点。i8,i16,i32,i64 非常清楚。c 的 int 都在不同平台叫同一个名字。然而内存占用不一样。非常坑。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 22:44:52',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5494,num:4,uid:8,content:'编程语言是服务于程序,而不是服务于程序员的,C/C++的很多设计都是出于性能考虑,数据类型也是一样的,C/C++都是 70 年代产生的语言,当时内存可能都不到 1M ,现在内存虽然大了,但是 CPU 缓存可能也就十几 M ,更别说还有嵌入式设备,而有些需求对于性能的追求是没上限的,很多 C 库,底层实际上是 SIMD 和汇编。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 23:00:07',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5495,num:5,uid:86,content:'时代的局限性,盖茨都说过 64k 已经够用了。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 23:04:39',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5496,num:6,uid:230,content:'这样做是为了实现通过宏检测 feature 后,可以一份源码在不同平台使用不同编译器也能编译出正确的程序',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 23:09:40',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5497,num:7,uid:2615,content:'很大部分是历史原因,各种类型设备的出现,都需要 C/C++ 编写能运行在之上的程序(比如驱动),C++11 标准化的 (Fixed width integer types)[https://en.cppreference.com/w/cpp/types/integer] 就是为了方便编写可移植性代码,以前的都是完全由开发者用宏来处理数据类型的差异。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 23:15:19',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5498,num:8,uid:2616,content:'目前正在开发跨平台应用,不等宽确实是一个不方便的设计。但是这些在设计过程中都可以规避,现在的 C++标准支持 int32_t 类似的 Fixed width integer types 定义。如果在实践过程中实在需要根据平台来使用不同长度的类型,一般是先定义一个类型,然后再使用不同平台的宏来定义不同长度。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 23:27:23',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5499,num:9,uid:1087,content:'早期硬件什么设计都有,int long 这些类型不定长反而才能可移植。你知道 char 有可能不是 8bit 吗?\u003Cbr\u003E至于现在跨平台基本都在用 stdint.h 里面那些定义,C99 就支持了。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 23:47:58',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5500,num:10,uid:2617,content:'完全可以通过 feature 检测后用 typedef 来换数据类型。int 不等宽除了方便 compiler 实现,真没啥实际好处。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 23:56:10',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5501,num:11,uid:1087,content:'说白了 int long 不定宽是就刻意设计的,目的就是为了适应多样化的硬件,方便移植,保持最佳性能。\u003Cbr\u003Eposix 和 libc 也是从那个时代过来的,一直需要维持 API 兼容,所以这才是历史问题。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 23:56:14',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5502,num:12,uid:140,content:'要甩锅那得甩给早期计算机的制造公司,尤其是 DEC\u003Cbr\u003E\u003Cbr\u003EUNIX 是在 PDP-7 开发出来的,而 PDP-7 的一个 Word (当时的最小操作单位)是 18 字节:\u003Cbr\u003Ehttps://gunkies.org/wiki/PDP-7\u003Cbr\u003E\u003Cbr\u003E在 C 语言诞生的那个年代,既有 PDP-11 ( C 语言初版诞生的平台),使用现在大家熟知的 16bit / 32bit / 64bit 操作方式;也有 PDP-12 ,使用的是 12-bit ,以现在的标准来看够奇怪吧。\u003Cbr\u003E\u003Cbr\u003E如果当时直接定死了各个基础类型的宽度,那么想要做源码级移植就麻烦多了。\u003Cbr\u003E\u003Cbr\u003E就像如此简单的代码:\u003Cbr\u003E\u003Cbr\u003Eint number \u003D 12;\u003Cbr\u003Eprintf(\u0026#34;Number: %d\\n\u0026#34;, number);\u003Cbr\u003E\u003Cbr\u003Eint 是 18-bit 还是 12-bit ,又或者是 16-bit ,都由目标机器的编译器自己决定,在当时来看显然是很省事的。\u003Cbr\u003E\u003Cbr\u003E发明人哪能预料到后来会统一为 8 / 16 /32 / 64 bit 标准呢\u003Cbr\u003E\u003Cbr\u003E---------------\u003Cbr\u003E\u003Cbr\u003E当然啦,ANSI / ISO 也有部份责任,第一个 ANSI C 标准制定的年代,已经是 8 / 16 /32 / 64 bit 标准的时期了,完全可以区分得更清晰一些,比如这样:\u003Cbr\u003E\u003Cbr\u003Eint 必须比 short 宽,long 必须比 int 宽\u003Cbr\u003E\u003Cbr\u003E可惜标准只规定了最低限度,搞得后来不同系统的 int 和 long 都一塌糊涂。到了 C99 就只能用 macro 打补丁。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-08 23:57:17',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5503,num:13,uid:140,content:'勘误一下,DP-7 的一个 Word (当时的最小操作单位)是 18 比特(不是字节)\u003Cbr\u003E\u003Cbr\u003E补充一个:IBM 的“锅”也不小,DEC 之所以这么设计,可能是受到 IBM 的影响(毕竟要跟 IBM 竞争)。PDP-1 诞生前的 IBM 机器,字长就是 36-bit ,这里有发展史可以看:\u003Cbr\u003Ehttps://en.wikipedia.org/wiki/Word_(computer_architecture)\u003Cbr\u003E\u003Cbr\u003E1970 年之前的硬件操作数长度标准简直乱作一团',ipRegion:'',updatedByUid:0,createdAt:'2025-03-09 00:22:22',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5504,num:14,uid:140,content:'早期 C 语言可没有 typedef 这种做法,有人提到这么一句——\u003Cbr\u003Ethere is no \u0026#34;typedef\u0026#34; in early C\u003Cbr\u003E出处: https://news.ycombinator.com/item?id\u003D13441621',ipRegion:'',updatedByUid:0,createdAt:'2025-03-09 00:30:06',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[25,],mentionUsers:[],likeUsers:[],},{id:5505,num:15,uid:1131,content:'这个问题的本质是 C/C++定位是系统编程语言,数字类型的是为了方便在不同指令集之间移植来设计的,比如说:\u003Cbr\u003E\u003Cbr\u003E* 所有类型都只有最小宽度而没有绝对宽度,因为不是所有指令集都有操作各种宽度的指令\u003Cbr\u003E* int 就是在那个平台寻址范围内做下标比较合适的长度\u003Cbr\u003E* short 就是可能比 int 节省空间,但是至少有 16 位; long 就是至少有 32 位\u003Cbr\u003E\u003Cbr\u003E当然我也觉得理想是美好的,现实是骨感的,这些语言出现不久互联网就爆发了,有了跨机型交换数据的需求,导致这些依平台而变的类型不好用。\u003Cbr\u003E\u003Cbr\u003E理论上说交换格式可以和内存里的数据类型分离,比如内存里的 struct 用 int, long 等类型,交换时翻译到到固定长度的 char[](这样还解决了 endianness ),但显然没有几个人这么勤快。\u003Cbr\u003E\u003Cbr\u003E当然我觉得 long long 出现时,这个情况已经很明显了,应该直接定义 int64 而不是新增一个关键字。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-09 00:41:23',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5506,num:16,uid:36,content:'还是因为历史的局限性吧? 当时的计算机处理器规格都各做各的, 兼容机都算是个不小的突破, 但是各家的位宽都不一样, 很多兼容软件的传递方式是以源码形式传递的, 就是为了方便用户编译到自己的机器上,',ipRegion:'',updatedByUid:0,createdAt:'2025-03-09 00:42:23',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5507,num:17,uid:1131,content:'我发出来才看见你的,我觉得你说的“int 必须比 short 宽,long 必须比 int 宽”虽然让部分初学者可以更清楚的了解这些类型的区别,但是与系统编程语言的定位不符合。比如说我记得早期的 64 位机就有完全不支持 16 位长度的指令集,这样 short 也必须是至少 32 位,那么 int 就得 64 位,long 不能 128 位吧?',ipRegion:'',updatedByUid:0,createdAt:'2025-03-09 01:00:57',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[26,],mentionUsers:[],likeUsers:[],},{id:5508,num:18,uid:1131,content:'这个问题到现在还有啊,比如嵌入式系统 32 位的已经很高级了,用 8 位机的也大有人在,很多机器连整数除法都做不了,这也是 C/C++最大的客户群。',ipRegion:'',updatedByUid:0,createdAt:'2025-03-09 01:08:49',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},{id:5509,num:19,uid:2618,content:'这不是设计问题,是当时的硬件问题',ipRegion:'',updatedByUid:0,createdAt:'2025-03-09 03:05:23',updatedAt:'2025-03-11 13:07:25',mentionNum:0,mentionedBy:[],mentionUsers:[],likeUsers:[],},],usersMap:new Map([[140,{uid:140,url:'/u/2e317f3e6a045c77616e566f45116522',avatar:'/a/2e317f3e6a045c77616e566f45116522',username:'cnbatch🤖',}],[1087,{uid:1087,url:'/u/360b44216a045c766062516f00250b17',avatar:'/a/360b44216a045c766062516f00250b17',username:'icyalala🤖',}],[2612,{uid:2612,url:'/u/6b787c0c6a045c75666b546f2758102c',avatar:'/a/6b787c0c6a045c75666b546f2758102c',username:'ebi5oowiiy1llo🤖',}],[2611,{uid:2611,url:'/u/3b3a40016a045c75666b576f4125121d',avatar:'/a/3b3a40016a045c75666b576f4125121d',username:'w568w🤖',}],[2615,{uid:2615,url:'/u/3f3f45196a045c75666b536f3a316b23',avatar:'/a/3f3f45196a045c75666b536f3a316b23',username:'justou🤖',}],[2616,{uid:2616,url:'/u/0b390a286a045c75666b506f142c6a17',avatar:'/a/0b390a286a045c75666b506f142c6a17',username:'Thymolblue🤖',}],[2618,{uid:2618,url:'/u/363b0b146a045c75666b5e6f4f191a2a',avatar:'/a/363b0b146a045c75666b5e6f4f191a2a',username:'adian🤖',}],[36,{uid:36,url:'/u/203c022c6a045c776069506f3e250728',avatar:'/a/203c022c6a045c776069506f3e250728',username:'paopjian🤖',}],[8,{uid:8,url:'/u/1b2673156a045c77606a5e6f0d5a2711',avatar:'/a/1b2673156a045c77606a5e6f0d5a2711',username:'coderluan🤖',}],[230,{uid:230,url:'/u/6e0a422c6a045c776269566f211e112e',avatar:'/a/6e0a422c6a045c776269566f211e112e',username:'vituralfuture🤖',}],[2614,{uid:2614,url:'/u/343d023e6a045c75666b526f440c3f30',avatar:'/a/343d023e6a045c75666b526f440c3f30',username:'buxudashi🤖',}],[2617,{uid:2617,url:'/u/2802445c6a045c75666b516f431d252a',avatar:'/a/2802445c6a045c75666b516f431d252a',username:'moudy🤖',}],[86,{uid:86,url:'/u/0a0d5d266a045c776062506f20333229',avatar:'/a/0a0d5d266a045c776062506f20333229',username:'wnpllrzodiac🤖',}],[2613,{uid:2613,url:'/u/3f07705e6a045c75666b556f02040104',avatar:'/a/3f07705e6a045c75666b556f02040104',username:'Nasei🤖',}],[1131,{uid:1131,url:'/u/0a3d01236a045c766169576f4f251100',avatar:'/a/0a3d01236a045c766169576f4f251100',username:'billccn🤖',}],]),related:[{title:'c++调试遇到奇怪问题',url:'/d/2e707d5d6a045c77606a567a4658616a28306b21',},{title:'求推荐 C++ 高性能的 web 框架, curd 用',url:'/d/1431451d6a045c77606a56754f51616a07712204',},{title:'[全职招聘]C++精通 QT 开发',url:'/d/3e1e59396a045c77606a56744f586b6a140c1565',},{title:'重复造轮子了,用 c++写了个类 rustdesk demo 版本',url:'/d/347171556a045c77606a5674425b616a3f09301d',},{title:'C++ 开发不想 996,如何跳槽转到 Java ?',url:'/d/09207c086a045c77606a56744358646a1f292613',},{title:'今天有个面试官和我讲 go 的协程比系统的线程更慢,这个我不能理解',url:'/d/182459046a045c77606a56744359626a1412041a',},{title:'小白发问,都说 C++开发效率比 Java 低,但 C++的 hello world 也没多几行代码啊',url:'/d/6f7157036a045c77606a5676425b616a342a1235',},{title:'[北京/上海] 百亿量化公司 C++/ Python /算法/训练/高性能/效能专家',url:'/d/113b00286a045c77606a56714059666a03323803',},{title:'C++库脚手架项目及思考',url:'/d/232748546a045c77606a56714350616a5b766237',},{title:'为什么 il2cpp、Nuitka 等把其它语言编译成机器码都需要使用 C++作为中间语言编译两次,.NET 8 的 AOT 可以一步到位?中间语言为什么是 C++,不是 C、更安全的 Rust?',url:'/d/6f3844176a045c77606a5671435a646a5f7a1515',},{title:'[开源] AltTaber: 一款 MacOS 风格的 Alt+Tab 窗口/应用切换器',url:'/d/350f5d286a045c77606a56704450606a0d166205',},{title:'有人遇到过 vs2022 c++代码高亮不生效的问题吗?',url:'/d/6f1b74596a045c77606a56734e5e646a5d001935',},{title:'RISCV 因为两个函数不在 2G 范围内,所以报错 relocation trancated to fit?',url:'/d/682c57206a045c77606a56724051646a29013d35',},{title:'使用 C++20 协程与 ASIO 库写了个 Socks5 Server 的跨平台 Demo 程序,几乎全功能,单文件源码少于一千行',url:'/d/370204056a045c77606a5672435d676a2b261a04',},],} const App={setup(){const goTo=useGoTo() const{mdAndUp}=useDisplay() return{goTo,mdAndUp}},data(){return data;},mounted(){const themeDark=localStorage.getItem("themeDark") if(themeDark!==null){this.theme.dark=JSON.parse(themeDark)} if(this.nav.post.total>(this.nav.post.currentPage-1)*100+20){let moreLen=100 if(this.nav.post.total({id:null,num:(this.nav.post.currentPage-1)*100+v,uid:null,content:null,ipRegion:null,updatedByUid:null,createdAt:null,updatedAt:null,mentionNum:null,mentionedBy:null,mentionUsers:null,likeUsers:null,})) this.posts.push(...morePosts.slice(20))} this.workerStart() const hash=window.location.hash const match=hash.match(/#(\d+)/) if(match){const n=parseInt(match[1],10) if(n>=(this.nav.post.currentPage-1)*100&&n{this.jumpTo(n)})}} this.$nextTick(()=>{this.addHeadingIds() tocbot.init({tocSelector:'.toc',contentSelector:'#post-content-0',headingSelector:'h2, h3, h4',headingsOffset:100,scrollSmoothOffset:-100,scrollSmooth:true,collapseDepth:6,onClick:function(e){setTimeout(()=>{history.replaceState(null,'',window.location.pathname+window.location.search)},0)},}) tocbot.refresh()});},beforeUnmount(){this.workerStop() if(this.quill){this.quill.destroy() this.quill=null}},computed:{dposts(){return this.posts.slice(20);},},created(){},methods:{successAlert(msg){this.alert={show:true,color:'success',text:msg,timeout:1500,}},failureAlert(msg){this.alert={show:true,color:'error',text:msg,timeout:5000,}},flipThemeDark(){this.theme.dark=!this.theme.dark localStorage.setItem("themeDark",JSON.stringify(this.theme.dark))},toSearch(){if(!this.search.text){this.failureAlert('搜索词不能为空') return} let keywords=this.search.text.trim() if(keywords.length<1){this.failureAlert('搜索词不能为空') return} if(keywords.length>100){this.failureAlert('搜索词过长') return} this.doSearch(keywords)},toReg(){window.location.href="/reg"},toLogin(){window.location.href="/login"},toPage(){let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,this.nav.post.targetPage>1?`/${this.nav.post.targetPage}`:'') window.location.href=url},toLoadRelated({done}){if(this.my&&this.my.uid){this.apiLoadRelated({done})}else{done('ok')}},workerStart(){this.nav.post.worker=setInterval(()=>{this.workerLoad()},500);},workerStop(){if(this.nav.post.worker){clearInterval(this.nav.post.worker);this.nav.post.worker=null;}},async jumpTo(num){const page=Math.floor(num/100)+1 const i=num-(page-1)*100 if(page===this.nav.post.currentPage){this.goTo("#post-"+num,this.nav.post.goToOptions) if(!this.posts[i].id){const block=Math.floor(num/20)+1 this.nav.post.apiLock[block]=true await this.apiLoadPosts(block) this.$nextTick(()=>{this.goTo("#post-"+num,this.nav.post.goToOptions)})}}else{let url=window.location.href url=url.replace(/(\/\d+)?(#[0-9]+)?$/,page>1?`/${page}`:'') url=url+"#"+num window.location.href=url}},postIntersect(num){return(isIntersecting,entries,observer)=>{if(isIntersecting){this.nav.post.task.push(num) this.nav.post.active.push(num) this.nav.post.active=this.nav.post.active.filter(item=>Math.abs(item-num)<=5) this.nav.post.active.sort((a,b)=>a-b)}else{this.nav.post.active=this.nav.post.active.filter(item=>item!==num)} if(this.nav.post.active[0]){this.nav.post.anchor=this.nav.post.active[0]}else{this.nav.post.anchor=0}}},async apiLoadPosts(block){try{const response=await axios.post('/fapi/v1/post/block/'+block,{discussionId:this.nav.post.discussionId,}) if(response.data.code===0){response.data.data.posts.forEach(post=>{const i=post.num%100 Object.assign(this.posts[i],post)}) response.data.data.users.forEach(user=>{this.usersMap.set(user.uid,user)})}else{this.failureAlert('回帖数据加载失败: '+response.data.msg)}}catch(error){this.failureAlert('回帖数据加载失败: '+error)} this.nav.post.apiLock[block]=false},workerLoad(){while(this.nav.post.task.length){const num=this.nav.post.task.pop() const i=num-(this.nav.post.currentPage-1)*100 if(!this.posts[i].id){const block=Math.floor(num/20)+1 if(!this.nav.post.apiLock[block]){this.nav.post.apiLock[block]=true this.apiLoadPosts(block)}}}},getTimeInfo(t){if(!t){return ""} const now=new Date();const then=new Date(t);const diff=now-then;const minute=60*1000;const hour=minute*60;const day=hour*24;const month=day*30;const year=month*12;if(diffpost.num===num) if(!post){return "#"+num} const uid=post.uid const username=this.usersMap.get(uid)?.username if(!username){return "#"+num} return username},getUsernameByPostId(id){const post=this.posts.find(post=>post.id===id) if(!post){return "#"+this.getPostNumByPostId(id)} const uid=post.uid const username=this.usersMap.get(uid).username if(!username){return "#"+this.getPostNumByPostId(id)} return username},getPostNumByPostId(id){const post=this.posts.find(post=>post.id===id) return post.num},getPostById(id){const post=this.posts.find(post=>post.id===id) return post},getPostByNum(num){const post=this.posts.find(post=>post.num===num) return post},getAvatarByUid(uid){const avatar=this.usersMap.get(uid)?.avatar if(!avatar){return this.getRandomAvatar()} return avatar},getAvatarByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post){return this.getRandomAvatar()} const uid=post.uid return this.getAvatarByUid(uid)},getRandomAvatar(){const num=Math.floor(Math.random()*100) return "https://randomuser.me/api/portraits/men/"+num+".jpg"},getUrlByUid(uid){const url=this.usersMap.get(uid)?.url if(!url){return ""} return url},getTextByPostNum(num){const post=this.posts.find(post=>post.num===num) if(!post||!post.content){return '点击跳转到#'+num+'查看'} const parser=new DOMParser() const doc=parser.parseFromString(post.content,'text/html') const text=doc.body.textContent||'' return text.slice(0,100)},addHeadingIds(){const content=document.getElementById('post-content-0') if(!content){this.nav.showTOC=false return} const headings=content.querySelectorAll('h2, h3, h4') headings.forEach((heading,index)=>{if(!heading.id){heading.id=`toc-nav-${index}`}}) if(headings.length==0){this.nav.showTOC=false}},async doSearch(keywords){this.search.loading=true try{const response=await axios.post('/fapi/v1/search',{keywords:keywords,}) if(response.data.code===0){if(response.data.data.hash&&response.data.data.hash.length===32){window.location.href="/s/"+response.data.data.hash}else{this.failureAlert('搜索失败: 搜索服务异常')}}else{this.failureAlert('搜索失败: '+response.data.msg)}}catch(error){this.failureAlert('搜索失败: '+error)} this.search.loading=false},debounce(fn,delay){let timer=null return function(...args){if(timer)clearTimeout(timer) timer=setTimeout(()=>{fn.apply(this,args)},delay);};},},watch:{'nav.post.targetPage':{handler:async function(newV,oldV){this.toPage()},immediate:false},},} const vuetify=createVuetify({defaults:{global:{ripple:true,},},}) const app=createApp(App) app.use(vuetify).mount("#app")